HttpGet响应字符串被截断

时间:2014-06-23 06:57:08

标签: android http-get androidhttpclient

我正在使用httpGet。但我的反应不完整。只是为了测试我使用了另一个URL,为此我的代码工作正常,但对于我的实际链接,我的响应被截断。我提到了this解决方案,但它对我没有用。请建议我如何解决这个问题。

这就是我所做的:

private HttpGet httpGet;
private HttpClient client;
private String url;

httpGet = new HttpGet(url);
try{
    BasicResponseHandler responseHandler = new BasicResponseHandler();
    data = client.execute(httpGet, responseHandler);

    Log.i(LOG_TAG, "Response :: " + data);

    returnClass = getMapper().readValue(data, responseType);
    Log.i(LOG_TAG, "URL :: " + url + " Completed");
}finally{
    client.getConnectionManager().shutdown();
}

1 个答案:

答案 0 :(得分:0)

也许您的消息太长,无法显示logcat。

您可能正在获得完整的HTTP答案,但logcat只向您显示字符串的一部分。

修改:正如我在评论中所说,您可以尝试使用HttpURLConnection代替HttpClientHttpGet。如下例所示:

HttpURLConnection urlConnection = null;
BufferedReader reader = null;

<强> ...

try {
    URL url = new URL("Your URL here");
    // Open the connection
    urlConnection = (HttpURLConnection) url.openConnection();
    urlConnection.setRequestMethod("GET");
    urlConnection.connect();

    // Read the input stream into a String
    InputStream inputStream = urlConnection.getInputStream();
    StringBuffer buffer = new StringBuffer();
    if (inputStream == null) {
        // Nothing to do.
        return null;
    }
    reader = new BufferedReader(new InputStreamReader(inputStream));

    String line;
    while ((line = reader.readLine()) != null) {
        // Adding new line mark.
        buffer.append(line + "\n");
    }

    if (buffer.length() == 0) {
        // Stream was empty.
        return null;
    }
    return buffer.toString();
} catch (IOException e) {
    Log.e(LOG_TAG, "Error ", e);                
    return null;
} finally{
    if (urlConnection != null) {
        urlConnection.disconnect();
    }
    if (reader != null) {
        try {
            reader.close();
        } catch (final IOException e) {
            Log.e(LOG_TAG, "Error closing stream", e);
        }
    }
}