[英]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();
}
}
});
这是使用登录名时被调用的类:
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 / Http / Http1xStream $在okhttp3.internal.http.Http1xStream.newFixedLengthSink(Http1xStream.java:226)的FixedLengthSink在okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java)在okhttp3.internal.http.Http1xStream.createRequestBody(Http1xStream.java:98) 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。 okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)的okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)okok的http://Real.ceptorChain.proceed(RealInterceptorChain.java:92) okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)上的.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:124)okhttp3.internal.http.RealInterceptorChain.proceed(Realhttpcept3Chain.java:67) 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)在com.android.java.lang.reflect.Method.invokeNative(本机方法)在android.app.ActivityThread.main(ActivityThread.java:5017)在com.android.java.lang.reflect.Method.invoke(Method.java:515) .internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779)在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)在dalvik.system.NativeStart.main(本机方法)
我在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( https://code.google.com/p/android/issues/detail?id=82691 )时,Square库(okhttp,picasso)正在引发VerifyError。
检查gradle模块级别的构建文件,并在内部检查是否启用了Jack。 像这样:
jackOptions {
enabled true
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.