2016-04-27 8 views
3

Ich habe einen Fehler, der nur in Android 4 (API 16 bis 19) passiert.java.lang.ClassNotFoundException in AsyncTask auf API 16 bis 19

Ich verwende den folgenden Code

private class AcceptBooking extends AsyncTask<String, Void, String> { 
    int response; 

    @Override 
    protected void onPreExecute() { 
    } 

    @Override 
    protected void onPostExecute(String s) { 
     switch (response){ 
      case NO_UPDATE: 
       startApp(); 
       break; 
      case SUGEST_UPDATE: 
       adviceUpdate(); 
       break; 
      case FORCE_UPDATE: 
       forceUpdate(); 
       break; 
      default: 
       startApp(); 
       break; 
     } 

    } 

    @Override 
    protected String doInBackground(String... params) { 
     response = DBConnect.updateApp(); 

     return "done"; 
    } 
} 

Und wenn ich versuche, dieses AsyncTask ich folgende Fehlermeldung erhalten, auszuführen:

com.socialcar E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2 
java.lang.RuntimeException: An error occured while executing doInBackground() 
    at android.os.AsyncTask$3.done(AsyncTask.java:299) 
    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273) 
    at java.util.concurrent.FutureTask.setException(FutureTask.java:124) 
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 
    at java.lang.Thread.run(Thread.java:856) 
Caused by: java.lang.NoClassDefFoundError: com/socialcar/api_connection/DBConnect 
    at com.socialcar.SplashScreenActivity$AcceptBooking.doInBackground(SplashScreenActivity.java:154) 
    at com.socialcar.SplashScreenActivity$AcceptBooking.doInBackground(SplashScreenActivity.java:126) 
    at android.os.AsyncTask$2.call(AsyncTask.java:287) 
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 
    at java.lang.Thread.run(Thread.java:856) 
Caused by: java.lang.ClassNotFoundException: com.socialcar.api_connection.DBConnect 
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:501) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:461) 
    at com.socialcar.SplashScreenActivity$AcceptBooking.doInBackground(SplashScreenActivity.java:154) 
    at com.socialcar.SplashScreenActivity$AcceptBooking.doInBackground(SplashScreenActivity.java:126) 
    at android.os.AsyncTask$2.call(AsyncTask.java:287) 
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 
    at java.lang.Thread.run(Thread.java:856) 

Und das ist es, die Abhängigkeiten, die ich in gradle haben:

compile fileTree(include: ['*.jar'], dir: 'libs') 
    testCompile 'junit:junit:4.12' 
    compile 'com.android.support:appcompat-v7:23.2.0' 
    compile 'com.android.support:design:23.2.0' 
    compile 'com.facebook.android:facebook-android-sdk:[4,5)' 
    compile 'com.google.android.gms:play-services:7.8.0' 
    compile 'org.apache.httpcomponents:httpmime:4.2.3' 
    compile 'com.google.guava:guava-jdk5:17.0' 
    compile 'com.android.support:multidex:1.0.1' 
    compile 'com.mcxiaoke.volley:library-aar:1.0.0' 
    compile 'de.hdodenhof:circleimageview:2.0.0' 
    compile 'com.android.support:cardview-v7:23.0.+' 
    compile 'com.android.support:recyclerview-v7:23.0.+' 

Ich versuche in diesem und anderen Foren eine Lösung zu finden und anscheinend das com.android.suppo rt: appcompat-v7-Version kann das Problem sein, aber Änderungen, die nicht funktionieren. Und auch ich java.util.objects wird in Java 7 eingeführt und das kann auch Auswirkungen haben, aber ich bin mir nicht sicher, was zu tun ist, um das zu beheben.

Jede andere Idee wird willkommen sein.

EDIT

Hier ist die BDConnect Klasse, der den Fehler geben:

import android.content.pm.PackageInfo; 
import android.os.Build; 

import com.socialcar.BuildConfig; 
import com.socialcar.Config; 
import com.socialcar.booking.NavigationActivity; 
import com.socialcar.custom.SHA1; 
import com.socialcar.http.RequestMethod; 
import com.socialcar.http.RestClient; 
import com.socialcar.model.Booking; 
import com.socialcar.model.BookingList; 
import com.socialcar.model.LoginResult; 
import com.socialcar.model.Token; 

import org.json.JSONObject; 

import java.io.IOException; 
import java.util.ArrayList; 
import java.util.List; 

/** 
* Created by PcCom1 on 14/03/2016. 
*/ 
public class DBConnect { 
    private static String URL = "https://someurl.com/"; 
    private static String SOCIAL_CAR_URL_OAUTH = URL + "test/auth"; 
    private static String SOCIAL_CAR_URL = URL + "test"; 
    private static String SOCIAL_CAR_URL_NO_V1 = URL + "test"; 
    private static String SOCIAL_CAR_URL_LOGIN = URL + "test/login"; 


    private static void checkResponseCode(RestClient restClient) throws IOException, IllegalStateException { 
     int responseCode = restClient.getResponseCode(); 
     if (responseCode == 500 || responseCode == 503) { 
      // ERRO no servidor 
     } else if (responseCode == 404) { 
      // ERRO pagina nao encontrada 
     } else if (responseCode == 401) { 
      if (restClient.getResponse().contains("Invalid JWT")){ 
       throw new IOException("JWT"); 
      } 
     } else if (responseCode != 200) { 
      // ERRO se a resposta n?o for a correta 
      throw new IOException(); 
     } 

    } 

    public static int updateApp(){ 
     String response = ""; 
     try { 
      RestClient restClient = new RestClient(SOCIAL_CAR_URL+"/mobile/updates"); 
      String model = Build.MODEL; 
      int version = Build.VERSION.SDK_INT; 
      int versionCode = BuildConfig.VERSION_CODE; 
      JSONObject data = new JSONObject(); 
      data.put("mobile", model); 
      data.put("sdk-version", version); 
      data.put("version-code", versionCode); 
      restClient.setJsonObject(data); 
      restClient.execute(RequestMethod.POST); 
      checkResponseCode(restClient); 
      response = restClient.getResponse(); 
      JSONObject jsonObject = new JSONObject(response); 
      return jsonObject.getInt("update"); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      return 0; 
     } 
    } 

    public static Token refreshToken(String refreshToken){ 
     String response = ""; 
     Token token = new Token(); 
     try { 
      //password = SHA1.toSHA1(password); 
      RestClient restClient = new RestClient(SOCIAL_CAR_URL_NO_V1+"/token-refresh"); 
      JSONObject data = new JSONObject(); 
      data.put("_token", refreshToken); 
      restClient.setJsonObject(data); 
      restClient.execute(RequestMethod.POST); 
      checkResponseCode(restClient); 
      response = restClient.getResponse(); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      // Error de conexión 
      return null; 
     } 
     try { 
      JSONObject jsonObject = new JSONObject(response); 
      token.setToken(jsonObject.getString("token")); 
      token.setRefreshToken(jsonObject.getString("refresh_token")); 
      return token; 
     } catch (Exception e) { 
      //error desconocido de parseo 
      e.printStackTrace(); 
      return null; 
     } 
    } 

    public static int logout(String email, String registration_id, String token){ 
     String response = ""; 
     try { 
      //password = SHA1.toSHA1(password); 
      RestClient restClient = new RestClient(SOCIAL_CAR_URL_OAUTH+"/user/logouts", token, null); 
      JSONObject data = new JSONObject(); 
      data.put("_username", email); 
      data.put("device_token", registration_id); 
      restClient.setJsonObject(data); 
      restClient.execute(RequestMethod.POST_TOKEN); 
      checkResponseCode(restClient); 
      response = restClient.getResponse(); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      // Error de conexión 
      return -999; 
     } 
     try { 
      JSONObject jsonObject = new JSONObject(response); 
      return jsonObject.getInt("error"); 

     } catch (Exception e) { 
      //error desconocido de parseo 
      e.printStackTrace(); 
      return -1000; 
     } 
    } 

    public static Token login(String email, String password){ 
     String response = ""; 
     Token token = new Token(); 
     try { 
      //password = SHA1.toSHA1(password); 
      RestClient restClient = new RestClient(SOCIAL_CAR_URL_NO_V1+"/login-check"); 
      JSONObject data = new JSONObject(); 
      data.put("_username", email); 
      data.put("_password", password); 
      restClient.setJsonObject(data); 
      restClient.execute(RequestMethod.POST); 
      checkResponseCode(restClient); 
      response = restClient.getResponse(); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      // Error de conexión 
      return null; 
     } 
     try { 
      JSONObject jsonObject = new JSONObject(response); 
      token.setToken(jsonObject.getString("token")); 
      if(jsonObject.has("refresh_token")) 
       token.setRefreshToken(jsonObject.getString("refresh_token")); 
      return token; 

     } catch (Exception e) { 
      //error desconocido de parseo 
      e.printStackTrace(); 
      return null; 
     } 
    } 

    public static LoginResult getUserInfo(String regId, Token token){ 
     String response = ""; 
     LoginResult loginResult; 
     try { 

      RestClient restClient = new RestClient(
        SOCIAL_CAR_URL_OAUTH+"/user/info?device_token=" + regId, 
        token.getToken(), null); 
      restClient.execute(RequestMethod.GET_TOKEN); 
      checkResponseCode(restClient); 
      response = restClient.getResponse(); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      loginResult = new LoginResult(); 
      // Error de conexión 
      loginResult.setError(999); 
      return loginResult; 
     } 
     try { 

      loginResult = JSONParser.getLoginResultFromJson(response); 


      return loginResult; 

     } catch (Exception e) { 
      //error desconocido de parseo 
      e.printStackTrace(); 
      loginResult = new LoginResult(); 
      loginResult.setError(1000); 
      return loginResult; 
     } 
    } 

    public static Token loginFacebook(String facebookId, String facebookToken, String email, String regId){ 
     String response = ""; 
     Token token = new Token(); 
     try { 
      RestClient restClient = new RestClient(SOCIAL_CAR_URL_LOGIN+"/facebooks", null, facebookId); 
      JSONObject data = new JSONObject(); 
      data.put("email", email); 
      data.put("facebook_id", facebookId); 
      data.put("token-facebook", facebookToken); 
      restClient.setJsonObject(data); 
      restClient.execute(RequestMethod.POST_OAUTH); 
      checkResponseCode(restClient); 
      response = restClient.getResponse(); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      return null; 
     } 
     try { 
      JSONObject jsonObject = new JSONObject(response); 
      if (jsonObject.has("token")){ 
       token.setToken(jsonObject.getString("token")); 
       token.setRefreshToken(jsonObject.getString("refresh_token")); 
       token.setError(0); 
       return token; 
      } 
      token.setError(jsonObject.getInt("error")); 
      return token; 


     } catch (Exception e) { 
      //error desconocido de parseo 
      e.printStackTrace(); 
      return null; 
     } 
    } 

    public static Token loginGooglePlus(String googleId, String googleToken, String email, String regId){ 
     String response = ""; 
     Token token = new Token(); 
     try { 
      RestClient restClient = new RestClient(SOCIAL_CAR_URL_LOGIN+"/googles", null, googleId); 
      JSONObject data = new JSONObject(); 
      data.put("email", email); 
      data.put("google_id", googleId); 
      data.put("token_google", googleToken); 
      restClient.setJsonObject(data); 
      restClient.execute(RequestMethod.POST_OAUTH); 
      checkResponseCode(restClient); 
      response = restClient.getResponse(); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      return null; 
     } 
     try { 

      JSONObject jsonObject = new JSONObject(response); 
      if (jsonObject.has("token")){ 
       token.setToken(jsonObject.getString("token")); 
       token.setRefreshToken(jsonObject.getString("refresh_token")); 
       token.setError(0); 
       return token; 
      } 
      token.setError(jsonObject.getInt("error")); 
      return token; 

     } catch (Exception e) { 
      //error desconocido de parseo 
      e.printStackTrace(); 
      return null; 
     } 
    } 

    public static BookingList getPendingBookingList(int ownerId, String token){ 
     String response = ""; 
     BookingList bookingList; 
     try { 
      RestClient restClient = new RestClient(SOCIAL_CAR_URL_OAUTH+"/booking/pendings/"+ownerId, token, null); 
      restClient.execute(RequestMethod.GET_TOKEN); 
      checkResponseCode(restClient); 
      response = restClient.getResponse(); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      if(e.getMessage() == "JWT"){ 
       bookingList = new BookingList(); 
       // Error de conexión 
       bookingList.setError(Config.JWT_EXPIRED); 
       bookingList.setBookings(new ArrayList<Booking>()); 
       return bookingList; 
      } 
      bookingList = new BookingList(); 
      // Error de conexión 
      bookingList.setError(999); 
      bookingList.setBookings(new ArrayList<Booking>()); 
      return bookingList; 
     } 
     try { 

      bookingList = JSONParser.getBookingListFromJson(response); 
      return bookingList; 

     } catch (Exception e) { 
      //error desconocido de parseo 
      e.printStackTrace(); 
      bookingList = new BookingList(); 
      bookingList.setError(1000); 
      bookingList.setBookings(new ArrayList<Booking>()); 
      return bookingList; 
     } 
    } 

    public static Booking getBookingDetailsOwner(int booking_line, String token){ 
     String response = ""; 
     Booking booking; 
     try { 
      RestClient restClient = new RestClient(SOCIAL_CAR_URL_OAUTH+"/booking/owner_details/"+booking_line, token, null); 
      restClient.execute(RequestMethod.GET_TOKEN); 
      checkResponseCode(restClient); 
      response = restClient.getResponse(); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      if(e.getMessage() == "JWT"){ 
       booking = new Booking(); 
       // Error de conexión 
       booking.setError(Config.JWT_EXPIRED); 
       return booking; 
      } 
      booking = new Booking(); 
      // Error de conexión 
      booking.setError(999); 
      return booking; 
     } 
     try { 

      booking = JSONParser.getBookingFromJson(response); 


      return booking; 

     } catch (Exception e) { 
      //error desconocido de parseo 
      e.printStackTrace(); 
      booking = new Booking(); 
      //loginResult.setError(1000); 
      return booking; 
     } 
    } 

    public static int cancelBookingByOwner(int booking_line, String reason, String description, String token){ 
     String response = ""; 
     try { 
      RestClient restClient = new RestClient(SOCIAL_CAR_URL_OAUTH+"/booking/cancels/"+booking_line, token, null); 
      JSONObject data = new JSONObject(); 
      data.put("reason", reason); 
      data.put("reason_description", description); 
      restClient.setJsonObject(data); 
      restClient.execute(RequestMethod.POST_TOKEN); 
      checkResponseCode(restClient); 
      response = restClient.getResponse(); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      if(e.getMessage() == "JWT") { 
       return Config.JWT_EXPIRED; 
      } 
      // Error de conexión 
      return 999; 
     } 
     try { 

      int error = JSONParser.getErrorResultFromJson(response); 


      return error; 

     } catch (Exception e) { 
      //error desconocido de parseo 
      e.printStackTrace(); 
      //loginResult.setError(1000); 
      return 1000; 
     } 
    } 

    public static int acceptBookingByOwner(int booking_line, String token){ 
     String response = ""; 
     try { 
      RestClient restClient = new RestClient(SOCIAL_CAR_URL_OAUTH+"/booking/accepts/"+booking_line, token, null); 
      restClient.execute(RequestMethod.GET_TOKEN); 
      checkResponseCode(restClient); 
      response = restClient.getResponse(); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      // Error de conexión 
      return 999; 
     } 
     try { 
      int error = JSONParser.getErrorResultFromJson(response); 
      return error; 

     } catch (Exception e) { 
      //error desconocido de parseo 
      e.printStackTrace(); 
      if(e.getMessage() == "JWT") { 
       return Config.JWT_EXPIRED; 
      } 
      //loginResult.setError(1000); 
      return 1000; 
     } 
    } 

} 

und die gradle:

android { 
    compileSdkVersion 23 
    buildToolsVersion "23.0.2" 
    useLibrary 'org.apache.http.legacy' 
    defaultConfig { 
     applicationId "com.socialcar" 
     minSdkVersion 16 
     targetSdkVersion 23 
     multiDexEnabled true 
     versionCode 33 
     versionName "3.0.1" 
    } 
    dexOptions { 
     javaMaxHeapSize "4g" 
    } 
    packagingOptions { 
     exclude 'META-INF/NOTICE.txt' // will not include NOTICE file 
     exclude 'META-INF/LICENSE.txt' // will not include LICENSE file 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
} 

repositories { 
    mavenCentral() 
} 

dependencies { 
    compile fileTree(include: ['*.jar'], dir: 'libs') 
    testCompile 'junit:junit:4.12' 
    compile 'com.android.support:appcompat-v7:17.2.0' 
    compile 'com.android.support:design:23.2.0' 
    compile 'com.facebook.android:facebook-android-sdk:[4,5)' 
    compile 'com.google.android.gms:play-services:7.8.0' 
    compile 'org.apache.httpcomponents:httpmime:4.2.3' 
    compile 'com.google.guava:guava-jdk5:17.0' 
    compile 'com.android.support:multidex:1.0.1' 
    compile 'com.mcxiaoke.volley:library-aar:1.0.0' 
    compile 'de.hdodenhof:circleimageview:2.0.0' 
    compile 'com.android.support:cardview-v7:23.0.+' 
    compile 'com.android.support:recyclerview-v7:23.0.+' 
} 

Manifest:

<application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 

     <meta-data 
      android:name="com.google.android.gms.version" 
      android:value="@integer/google_play_services_version"/> 

     <meta-data 
      android:name="com.facebook.sdk.ApplicationId" 
      android:value="@string/facebook_app_id"/> 

     <activity 
      android:name=".SplashScreenActivity" 
      android:label="@string/app_name" 
      android:screenOrientation="portrait"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".login.LoginActivity" 
      android:label="@string/app_name" 
      android:configChanges="orientation" 
      android:screenOrientation="portrait"> 
     </activity> 
     <activity 
      android:name=".booking.NavigationActivity" 
      android:label="@string/action_booking" 
      android:theme="@style/AppTheme.NoActionBar" 
      android:configChanges="orientation" 
      android:screenOrientation="portrait"> 
     </activity> 

     <activity android:name="com.facebook.FacebookActivity" 
      android:configChanges= 
       "keyboard|keyboardHidden|screenLayout|screenSize|orientation" 
      android:theme="@android:style/Theme.Translucent.NoTitleBar" 
      android:label="@string/app_name" 
      android:screenOrientation="portrait"/> 

     <receiver 
      android:name="com.socialcar.gcm.GcmReceiver" 
      android:permission="com.google.android.c2dm.permission.SEND"> 
      <intent-filter> 
       <action android:name="com.google.android.c2dm.intent.RECEIVE"/> 
       <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> 
       <category android:name="com.socialcar"/> 
      </intent-filter> 
     </receiver> 
     <service 
      android:name="com.socialcar.gcm.GcmMessageHandler" 
      android:exported="false"> 
      <intent-filter> 
       <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
      </intent-filter> 
     </service> 
    </application> 
+0

Es scheint, dass das Problem von Ihrer DBConnect-Klasse ist! hast du es richtig definiert? – pooyan

+0

set replace return "done", um null zurückzugeben –

+0

der Fehler schlägt vor, es ist nicht in der Lage, Ihre "DBConnect" -Klasse zu finden. Wenn es ordnungsgemäß definiert wurde, versuchen Sie, die App zu säubern und neu zu erstellen. –

Antwort

2

Fügen Sie diese Zeile in build.gradle:

compile 'com.android.support:multidex:1.0.1'

nun in der Anwendung Tag in Ihrem Mainfest diese Zeile hinzufügen,

android:name="android.support.multidex.MultiDexApplication"

Dann die folgende Klasse erstellen:

public class MultiDexApplication extends Application { 
public MultiDexApplication() { } 
    protected void attachBaseContext(Context base) { super.attachBaseContext(base); 
MultiDex.install(this); 
} 
    } 
+0

Du bist mein neuer Gott. – Ricardo

+0

@Ricardo, denke du hast den Kommentar übertrieben. : P –

+0

Das funktionierte für mich, aber es ist nicht gut erklärt. Fügen Sie die erste Zeile in 'dependencies' unter' build.gradle' hinzu. Fügen Sie die zweite Zeile innerhalb des Anwendungs-Tags unter AndroidManifest hinzu. Schließlich erstellen Sie einfach eine Klasse wie definiert. – 0014

Verwandte Themen