Issue
I'm trying to call a web service I made using Jersey. I tested the services by calling them from a console java app, and its working perfectly fine.
When calling from Android, it first gave me android.os.NetworkOnMainThreadException, I searched and found out that i should use AsyncTask to solve that,
the following code crashes when "response.getEntity(String.class)" is called while calling a get service, if i return the status ( response.getStatus()) instead of getEntity, it works fine. But the same thing doesnt work in case of a post request. getEntity() crashes on both
public String postTest(String arg){
Log.d("Service", "postRegister1");
client = Client.create();
WebResource service=client.resource(getBaseURI());
Log.d("Service", "postRegister2");
Form form = new Form();
form.add("arg", "lol");
int status;
ClientResponse response=null;
try{
response = service.path("test").get(ClientResponse.class);
//post
// response = service.path("post").path("test").post(ClientResponse.class,from);
Log.d("Service", "postRegister3");
status = response.getStatus();
//comment out the following if statement and the code will work perfectly fine, returning the stats
if(status == 200){
return response.getEntity(String.class);
}
return String.valueOf(response.getStatus());
}
catch(ClientHandlerException e){
Log.d("Exception1",e.getMessage()+ " ");
}
catch(UniformInterfaceException e){
Log.d("Exception2",e.getMessage()+ " ");
}
catch(Exception e){
Log.d("Exception3",e.getMessage()+ " ");
}
return "exception";
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
Log.d("doInback", params[0]);
return postTest(params[0]);
}
the code above code is part of the class:
public class Service extends AsyncTask<String, Void, String>{
and is called like:
try {
alert(new Service().execute("lol").get(),view);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
the alert method just displays an alert.
Solution
Solved by using a library for implementing Async Http client.
The tutorial which i followed can be found here.
Answered By - Hazim
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.