android http class cast exception [closed]
I am getting a ClassCastException:java.lang.string from the following code
try {
se = new StringEntity(myJson);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
httppost.setEntity(se);
httppost.setHeader("Accept", "application/json");
httppost.setHeader("Content-type", "application/json");
ResponseHandler responseHandler = new BasicResponseHandler();
try {
HttpRespon开发者_运维技巧se response = httpclient.execute(httppost, responseHandler);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
I am using the apache libray. Is there a cast that I can't see, any help on the cause appreciated. Might it be the setEntity(se) call?
It would be really helpful if you could post the whole stack trace - we're kind of guessing otherwise.
That said, the line I'd take a look at is:
HttpResponse response = httpclient.execute(httppost, responseHandler);
It looks like the return type of httpClient.execute(HttpUriRequest, ResponseHandler)
depends on the type of ResponseHandler you pass in. Since you're passing in an instance of BasicResponseHandler
, which returns the response body as a String, I'm guessing that method invocation is going to return an instance of java.lang.String
.
You can try changing that line to something like:
String responseBody = httpClient.execute(httppost, responseHandler);
But, that's just a guess, given that we don't have a full stack trace.
精彩评论