【问题标题】:Android application crash on http post send with okhttp使用 okhttp 发送 http post 时 Android 应用程序崩溃
【发布时间】:2017-01-12 00:24:55
【问题描述】:

大家好,我正在尝试设置登录屏幕,我正在使用 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();
        }

    }
});

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

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;
        }
    }

}

这是我使用的网址:

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:致命异常:主进程: diplomski.thop.thopmobileclient,PID:21873 java.lang.VerifyError: okhttp3/internal/http/Http1xStream$FixedLengthSink 在 okhttp3.internal.http.Http1xStream.newFixedLengthSink(Http1xStream.java:226) 在 好的http3.internal.http.Http1xStream.createRequestBody(Http1xStream.java:98) 在 okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:45) 在 okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) 在 okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45) 在 okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) 在 okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) 在 okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:109) 在 okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) 在 okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) 在 okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93) 在 okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) 在 okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:124) 在 okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) 在 okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) 在 okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:170)
在 okhttp3.RealCall.execute(RealCall.java:60) 在 diplomski.thop.thopmobileclient.controller.requests.LoginRequest.sendLoginRequest(LoginRequest.java:28) 在 diplomski.thop.thopmobileclient.controller.implementation.LoginImpl.login(LoginImpl.java:12) 在 diplomski.thop.thopmobileclient.LoginActivity$1.onClick(LoginActivity.java:37) 在 android.view.View.performClick(View.java:4438) 在 android.view.View$PerformClick.run(View.java:18422) 在 android.os.Handler.handleCallback(Handler.java:733) 在 android.os.Handler.dispatchMessage(Handler.java:95) 在 android.os.Looper.loop(Looper.java:136) 在 android.app.ActivityThread.main(ActivityThread.java:5017) 在 java.lang.reflect.Method.invokeNative(Native Method) 在 java.lang.reflect.Method.invoke(Method.java:515) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 在 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'
}

【问题讨论】:

    标签: java android web-services rest okhttp3


    【解决方案1】:

    你在用 Jack 编译吗?我可以看到 Square 库(okhttp、picasso)在使用 Jack(https://code.google.com/p/android/issues/detail?id=82691) 时会引发 VerifyError。

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

       jackOptions {
         enabled true
       }
    

    【讨论】:

    • 我不认为我在使用它,除非它默认启用。我已将我的 build.gradle 包含在问题中。我应该在其中添加 jackOptions 吗?
    • 当我添加 jackOption 时,错误消失了。谢谢。
    • 嗯... Nema 问题。用户报告说 Jack 引起了问题,不确定添加 Jack 如何为您解决问题。但我很高兴你的错误消失了;)
    猜你喜欢
    • 2020-02-10
    • 2017-05-03
    • 1970-01-01
    • 2018-04-03
    • 1970-01-01
    • 2015-09-24
    • 2014-02-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多