1

I am using httpGet. But I am getting an incomplete response. Just for testing I used another URL and for that my code works fine, but for my actual link my response is truncated. I have referred the this solution, but it didn't work for me. Please suggest me how to solve this.

Here is what I have done:

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();
}
Community
  • 1
  • 1
qulfille
  • 208
  • 3
  • 13
  • Incomplete code. What is data? Further: what do you consider to be `the response`? You are not streaming the response in indeed. For the rest: there are so many examples on this site which show you how that has to be done. Just search a bit. – greenapps Jun 23 '14 at 07:22
  • data is my string variable. In log i m printing data but there only i am getting incomplete response. – qulfille Jun 23 '14 at 07:25
  • The logcat does not always show the complete text if the response is huge even if you're getting the complete response. – Shivam Verma Jun 23 '14 at 10:40
  • If your string is very large, logcat will not display all of it. You can either try breaking your string into peices of ~4k and see on logcat or just see the string length and be satisfied. – Kartik_Koro Jun 23 '14 at 10:43

1 Answers1

0

Perhaps your message is too long for the logcat to display.

You're probably getting the complete HTTP answer, but the logcat is showing to you only a portion of the String.

Edit: As I said in the comments, you can try to use HttpURLConnection instead of HttpClient and HttpGet. Like the following example:

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);
        }
    }
}
honk
  • 9,137
  • 11
  • 75
  • 83
Mauker
  • 11,237
  • 7
  • 58
  • 76
  • This does not really provide an answer to the question. You are just assuming what the problem might be. This was already done by @ShivamVerma in the comments on the question. Also, another comment already mentions what could be done the solve the problem. Can you provide any added value to the comments already given (for example, a full solution)? – honk Nov 02 '14 at 08:04
  • Sorry, I didn't see the comment. And well, when this happened to me, the problem was really elsewhere and I was mislead by the truncated message on the logcat. But if you think the problem really is in this portion of you code, have you considered using HttpURLConnection instead of HttpClient? I'll edit my answer with an example. – Mauker Nov 03 '14 at 16:25
  • Thank you for providing the code. Your answer looks much better now! – honk Nov 03 '14 at 17:05
  • You're welcome! And sorry for the first lame answer. I'm new around here haha. Tell me later if it worked! – Mauker Nov 03 '14 at 17:11
  • No need to excuse yourself. We all started some time. You have shown to be cooperative. This is very nice. By the way, I was not looking for the solution personally. I'm frequently reviewing posts of newcomers and try to give them a bit of start-up assistance. The idea behind this is to keep the quality of answers (and also questions) high on Stack Overflow. Thanks again! – honk Nov 03 '14 at 17:31
  • Oh. I see! Well, thanks for the tips, and I hope this answer can help someone. – Mauker Nov 03 '14 at 20:22