开发者

android http class cast exception [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜