Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我正在获取HttpResponse,但response.getEntity().getContent()显示NullPointerException_Java_Android_Http - Fatal编程技术网

Java 我正在获取HttpResponse,但response.getEntity().getContent()显示NullPointerException

Java 我正在获取HttpResponse,但response.getEntity().getContent()显示NullPointerException,java,android,http,Java,Android,Http,我将在下面的数据库中发布数据。我附上了我的剩余代码,请检查并清除我的问题 protected String doInBackground(String... urls) { String url = urls[0]; String result = ""; HttpResponse response = doResponse(url); if (response == null) { return result; } else {

我将在下面的数据库中发布数据。我附上了我的剩余代码,请检查并清除我的问题

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;
}
我在下一行得到
NullPointerException

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


我不了解实体和内容。任何人都可以帮助我???

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

JavaDoc for声明可以返回
null
值。因此,您应该始终检查这一点

例如,而不是:

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();
}
您可以这样做:

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

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

JavaDoc for声明可以返回
null
值。因此,您应该始终检查这一点

例如,而不是:

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();
}
您可以这样做:

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

#cybersam谢谢你的回答。但是你能告诉我如何检查吗。因为我是新来的。你能澄清我吗?#cybersam谢谢你兄弟。但是我已经为实体设置了值。然后我不知道为什么我没有实体值:(现在我更新了我的编码。你能澄清我吗?doResponse()在请求中设置实体,然后返回响应。这是一个没有实体的响应。谢谢兄弟,因为你的教导,我已经清除了。非常感谢:)我也明白。非常感谢:)#cybersam谢谢你的回答。但是你能告诉我如何检查吗。因为我是新来的。你能澄清我吗?#cybersam谢谢你兄弟。但是我已经为实体设置了值。然后我不知道为什么我没有实体值:(现在我更新了我的编码。你能澄清我吗?doResponse()在请求中设置实体,然后返回响应。这是一个没有实体的响应。谢谢你,兄弟,因为你的教导,我已经清除了。非常感谢:)我也理解了。非常感谢:)