Android 应用程序在 http 上崩溃 post 使用 okhttp 发送

Android application crash on http post send with okhttp

大家好,我正在尝试设置登录屏幕,我正在使用 Web 服务进行登录验证。 Web 服务在我的本地计算机上是 运行,它应该在登录成功时以用户 ID 响应。目前该应用程序拥有所有权限,因此它不是一个明显的问题。

我为此使用 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();
        }

    }
});

这是使用登录时调用的class:

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 我正在使用:

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 服务在本地主机上 运行,我使用的是模拟器,所以我发现我应该使用那个 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.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'
}

你在和 Jack 一起编译吗?我可以看到 Square 库(okhttp、picasso)在使用 Jack(https://code.google.com/p/android/issues/detail?id=82691).

时引发 VerifyError

检查 gradle 模块级构建文件,并在内部检查 Jack 是否已启用。像这样:

   jackOptions {
     enabled true
   }