InsaneCricket InsaneCricket - 2 months ago 41
reST (reStructuredText) Question

Android application crash on http post send with okhttp

Hello guys im trying to setup a login screen and I am using a web service for the login verification. The web service is running on my local machine and its supposed to respond with the user ID when the login is succesful. Currently the application has all permissions so its not a manifest problem.

I am using okhttp:3.4.1 for this.

This is the button that triggers the login method

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

}
});


This is the class that gets called when login is used:

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

}


This is the URL im using:

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


The web service is running on local host and im using an emulator so I have found that im supposed to use that ip.

Every time I click login I get to
try (Response response = client.newCall(request).execute())
and then I get the following crash message.


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)


I have very limited experience with android so I dont know how to fix this. Couldn't find a solution by googling the error.

Edit: Adding my 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'
}

Answer

Are you compiling with Jack? I can see that Square libraries (okhttp, picasso) are raising VerifyError when using Jack(https://code.google.com/p/android/issues/detail?id=82691).

Check you gradle module-level build file, and inside check if Jack is enabled. Something like this:

   jackOptions {
     enabled true
   }
Comments