I get an HTTP response, but response.getentity (). getcontent () display null pointer exception

Source: Internet
Author: User

Protected StringDoinbackground(String...URLs) {

Stringwr.=URLs[0];
StringResult= "";

HttpResponseResponse=Doresponse(wr.);

If (Response== Null) {
ReturnResult;
} Else {

Try {
Result=Inputstreamtostring(Response.GetEntity().GetContent());

} Catch (IllegalStateExceptionE) {
Log.E(TAG,E.Getlocalizedmessage(),E);

} Catch (IOExceptionE) {
                & nbsp   log. (tag, E. Getlocalizedmessage E                 }

    & nbsp       }

            return Result;         }

I want to send the data to Dadabase.here below I enclose my remaining codes.so please check and clear my problem.please

Private HttpResponseDoresponse(Stringwr.) {

HttpClientHttpClient= New Defaulthttpclient(Gethttpparams());

HttpResponseResponse= Null;

Try {
Switch (TaskType) {

CasePost_task:
HttpPostHttpPost= New HttpPost(wr.);
ADD parameters
HttpPost.Setentity(New Urlencodedformentity(Params));
Response=HttpClient.Execute(HttpPost);

Break;
CaseGet_task:
HttpGetHttpGet= New HttpGet(wr.);
Response=HttpClient.Execute(HttpGet);
Break;
}
} Catch (ExceptionE) {

Log.E(TAG,E.Getlocalizedmessage(),E);

}

ReturnResponse;
}







Private StringInputstreamtostring(InputStream Is) {

StringLine= "";
StringBuilderTotal= New StringBuilder();

Wrap a bufferedreader around the InputStream
BufferedReaderRd= New BufferedReader(New InputStreamReader(Is));

Try {
Read response until the end
While ((Line=Rd.ReadLine()) != Null) {
Total.Append(Line);
}
} Catch (IOExceptionE) {
                log e (tag, E getlocalizedmessage (), E);             }

        & nbsp   //Return full String
            return Total.         }

I get NullPointerException in the line below,

result = inputStreamToString(response.getEntity().getContent());.

I don't understand about entity and content.anyone can help me??

Workaround 1:

Your HTTP response has not been completed entity . That's why it's getEntity() returned null .

The JavaDoc of GetEntity () indicates that a null value can be returned. Therefore, you should always check the.

For example, instead of:

result = inputStreamToString(response.getEntity().getContent());

You can do this:

Final HttpentityEntity=Response.GetEntity();
If (Entity== Null) {
Log.w (tag, "the response has no entity."

    // note:this method would return "" in the case, so we mu The St check for so in OnPostExecute ().

    //do whatever are necessary here ...
} else {
    result = inputstreamtostring (entity. Getcontent
} /span>

I get an HTTP response, but response.getentity (). GetContent () displays a null pointer exception

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.