2016-09-04 54 views
0

你好傢伙即時嘗試設置登錄屏幕,我正在使用Web服務進行登錄驗證。 Web服務正在我的本地機器上運行,並且它在登錄成功時應該使用用戶標識進行響應。目前該應用程序具有所有權限,因此它不是一個明顯的問題Android應用程序崩潰在http post發送okhttp

我使用okhttp:3.4.1爲此。

這是觸發登錄方法

btnLogin.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     { 
      StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() 
        .permitAll().build(); 
      StrictMode.setThreadPolicy(policy); 
      //Intent mainMenu = new Intent(LoginActivity.this, MainMenu.class); 
      //LoginActivity.this.startActivity(mainMenu); 

      LoginImpl li = new LoginImpl(); 
      int userID = li.login(txtUsername.getText().toString(), txtPassword.getText().toString()); 

      new AlertDialog.Builder(LoginActivity.this) 
        .setTitle("TEST") 
        .setMessage("USER ID = " + userID) 
        .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int which) { 

         } 
        }) 
        .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int which) { 

         } 
        }) 
        .setIcon(android.R.drawable.ic_dialog_alert) 
        .show(); 
     } 

    } 
}); 

這是被調用時,使用登錄類的按鈕:

package diplomski.thop.thopmobileclient.controller.requests; 
import java.io.IOException; 
import com.google.gson.Gson; 
import diplomski.thop.thopmobileclient.controller.implementation.Urls; 
import diplomski.thop.thopmobileclient.model.User; 
import okhttp3.OkHttpClient; 
import okhttp3.Request; 
import okhttp3.RequestBody; 
import okhttp3.Response; 

public class LoginRequest { 

    OkHttpClient client; 

    public LoginRequest() { 
     client = new OkHttpClient(); 
    } 

    public int sendLoginRequest(String username, String password) { 
     User user = new User(username, password); 
     String requestJson = new Gson().toJson(user); 
     RequestBody body = RequestBody.create(Urls.JSON, requestJson); 
     Request request = new Request.Builder().url(Urls.LOGIN_URL).post(body).build(); 
     try (Response response = client.newCall(request).execute()) { 
      String returnResponse = response.body().string(); 
      if (returnResponse.equalsIgnoreCase("Fail")) { 
       return -1; 
      } else { 
       return Integer.parseInt(returnResponse); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
      return -1; 
     } 
    } 

} 

這是使用URL IM:

public static final String START_URL = "http://10.0.2.2:8080/ThopWebService/rest/"; 
public static final String LOGIN_URL = START_URL + "user/login"; 

Web服務正在本地主機上運行,​​並使用模擬器即時通訊,所以我發現即時通訊應該使用t帽子ip。

每當我點擊登錄,我到try (Response response = client.newCall(request).execute()),然後我得到以下的崩潰消息。

09-04 08:12:30.855 21873-21873/diplomski.thop.thopmobileclient E/AndroidRuntime: FATAL EXCEPTION: main Process: diplomski.thop.thopmobileclient, PID: 21873 java.lang.VerifyError: okhttp3/internal/http/Http1xStream$FixedLengthSink at okhttp3.internal.http.Http1xStream.newFixedLengthSink(Http1xStream.java:226) at okhttp3.internal.http.Http1xStream.createRequestBody(Http1xStream.java:98) at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:45) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:109) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:124) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:170)
at okhttp3.RealCall.execute(RealCall.java:60) at diplomski.thop.thopmobileclient.controller.requests.LoginRequest.sendLoginRequest(LoginRequest.java:28) at diplomski.thop.thopmobileclient.controller.implementation.LoginImpl.login(LoginImpl.java:12) at diplomski.thop.thopmobileclient.LoginActivity$1.onClick(LoginActivity.java:37) at android.view.View.performClick(View.java:4438) at android.view.View$PerformClick.run(View.java:18422) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method)

我與Android的經驗非常有限,所以我不知道如何解決這個問題。通過搜索錯誤找不到解決方案。

編輯:添加我的build.gradle

apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 24 
    buildToolsVersion "24.0.2" 

    defaultConfig { 
     applicationId "diplomski.thop.thopmobileclient" 
     minSdkVersion 19 
     targetSdkVersion 24 
     versionCode 1 
     versionName "1.0" 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 

} 

dependencies { 
    compile fileTree(include: ['*.jar'], dir: 'libs') 
    testCompile 'junit:junit:4.12' 
    compile 'com.android.support:appcompat-v7:24.2.0' 
    compile 'com.android.volley:volley:1.0.0' 
    compile group: 'com.google.code.gson', name: 'gson', version: '2.6.2' 
    compile 'com.squareup.okhttp3:okhttp:3.4.1' 
} 

回答

1

你跟傑克編譯?我可以看到Square庫(okhttp,picasso)在使用Jack時出現VerifyError(https://code.google.com/p/android/issues/detail?id=82691)。

檢查你的gradle模塊級構建文件,並檢查是否啓用了Jack。這樣的事情:

jackOptions { 
    enabled true 
    } 
+0

我不認爲我使用它,除非它的默認啓用。我在這個問題中包含了我的build.gradle。我應該在其中添加jackOptions嗎? – InsaneCricket

+0

當我添加jackOption時,錯誤消失了。謝謝。 – InsaneCricket

+0

呃...... Nema有問題。用戶報告傑克造成問題,不知道如何增加傑克爲你解決它。但我很高興你的錯誤消失了;) – Gudin