Java 我收到 HttpResponse 但 responce.getEntity().getContent() 显示 NullPointerException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23117263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 20:50:02  来源:igfitidea点击:

I am getting HttpResponse but responce.getEntity().getContent() shows NullPointerException

javaandroidhttp

提问by user3515851

protected String doInBackground(String... urls) {
    String url = urls[0];
    String result = "";

    HttpResponse response = doResponse(url);

    if (response == null) {
        return result;
    } else {

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

        } catch (IllegalStateException e) {
            Log.e(TAG, e.getLocalizedMessage(), e);

        } catch (IOException e) {
            Log.e(TAG, e.getLocalizedMessage(), e);
        }

    }

    return result;
}

I am going to post data into database here below i attached my remaining codes so please check and clear my problem please

我要将数据发布到下面的数据库中我附上了我剩余的代码所以请检查并清除我的问题

private HttpResponse doResponse(String url) {

    HttpClient httpclient = new DefaultHttpClient(getHttpParams());

    HttpResponse response = null;

    try {
        switch (taskType) {

        case POST_TASK:
            HttpPost httppost = new HttpPost(url);
            // Add parameters
            httppost.setEntity(new UrlEncodedFormEntity(params));
            response = httpclient.execute(httppost);

            break;
        case GET_TASK:
            HttpGet httpget = new HttpGet(url);
            response = httpclient.execute(httpget);
            break;
        }
    } catch (Exception e) {

        Log.e(TAG, e.getLocalizedMessage(), e);

    }

    return response;
}

private String inputStreamToString(InputStream is) {

    String line = "";
    StringBuilder total = new StringBuilder();

    // Wrap a BufferedReader around the InputStream
    BufferedReader rd = new BufferedReader(new InputStreamReader(is));

    try {
        // Read response until the end
        while ((line = rd.readLine()) != null) {
            total.append(line);
        }
    } catch (IOException e) {
        Log.e(TAG, e.getLocalizedMessage(), e);
    }

    // Return full string
    return total.toString();
}

I am getting NullPointerExceptionat following line,

我正在NullPointerException以下行,

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

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

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

我不了解实体和内容。有人可以帮助我吗??

采纳答案by cybersam

Your HTTP response has no entity. That is why getEntity()is returning null.

您的 HTTP 响应没有实体。这getEntity()就是返回的原因null

The JavaDoc for getEntity()states that a nullvalue can be returned. Therefore, you should always check for that.

getEntity()的 JavaDoc声明null可以返回一个值。因此,您应该始终检查它。

For example, instead of:

例如,而不是:

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

you can do this:

你可以这样做:

final HttpEntity entity = response.getEntity();
if (entity == null) {
    Log.w(TAG, "The response has no entity.");

    //  NOTE: this method will return "" in this case, so we must check for that in onPostExecute().

    // Do whatever is necessary here...
} else {
    result = inputStreamToString(entity.getContent());
}