我得到 http 响应,但 response.getEntity().getContent() 显示空指针异常

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;
        }

我要发送数据到 dadabase.here 下面我附上我剩余的 codes.so 请检查并清除我的 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();
        }

我得到 NullPointerException 在下面的行,

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

我不明白关于 entity 和 content.anyone 可以帮助我??

解决方法 1:

您的 HTTP 响应已经没有 entity 。这就是为什么 getEntity() 返回 null 。

GetEntity()的 JavaDoc 指出, null 可以返回值。因此,您应始终检查的。

例如,而不是:

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

你可以这样做:

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());
}
posted @ 2016-05-12 08:04  流浪随风  阅读(35043)  评论(0编辑  收藏  举报