jfdoming
jfdoming

Reputation: 975

HttpURLConnection.getContentLength() returns -1

I've been struggling with this for several days now, but I can't seem to solve it.

After creating a HttpURLConnection to "http://goodmorningmartingrove.blogspot.ca/feeds/posts/default?alt=json", I am trying to read the data so I can convert it to a JSONObject, but the method getContentLength() returns -1. I know that this is because the connection cannot calculate the length, but I am not sure how to go about reading the stream without a length for the buffer array in is.read(buf);.

I have tried this:

int nextCharacter;
String responseData = "";
while(true){
    nextCharacter = reader.read();
    if(nextCharacter == -1)
        break;
        responseData += (char) nextCharacter;
}

But the read is extremely slow. This is explained here, but it doesn't explain how to speed it up for my purposes. I really don't know what to do, so any help would be greatly appreciated. Thanks!

EDIT

I got a NullPointerException, but the line numbers are inaccurate. Any idea where length() is called?

Code:

172 | String responseData = "";
173 | while ((responseData = reader.readLine()) != null);
174 | reader.close();

Exception:

java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
        at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
        at org.json.JSONTokener.nextValue(JSONTokener.java:94)
        at org.json.JSONObject.<init>(JSONObject.java:156)
        at org.json.JSONObject.<init>(JSONObject.java:173)
        at com.mci.mymci.main.TempFakeRSS$PostGetterTask.doInBackground(TempFakeRSS.java:180)
        at com.mci.mymci.main.TempFakeRSS$PostGetterTask.doInBackground(TempFakeRSS.java:155)
        at android.os.AsyncTask$2.call(AsyncTask.java:288)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
        at java.lang.Thread.run(Thread.java:818)

EDIT #2

I managed to resolve the problem. The exception was because the variable responseData was being reassigned each time the while loop iterated.

For others here is the code:

String line, responseData = "";
while (true) {
    line = reader.readLine();
    if(line == null) break;
        responseData += line;
}
reader.close();

Upvotes: 0

Views: 882

Answers (1)

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85779

Use a BufferedReader instead:

StringBuilder data = new StringBuilder();
BufferedReader br = null;
try {
    br = new BufferedReader(new InputStreamReader(yourInputStream));
    String line = "";
    while ( (line = br.readLine()) != null) {
        //do your processing here...
        data.append(line);
    }
} catch (...) {
    //handle exceptions...
} finally {
    if (br != null) {
        br.close();
    }
}
//the whole String content from the response is concatenated and stored in `data`
System.out.println(data.toString());

Upvotes: 1

Related Questions