Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpResponse to string length 0 is returned

Tags:

java

android

I am trying to make an HTTPGet request on a server in an Android APP, but when I change the httpresponse in string, the string is 0 length.

DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://"+server+path+"?assets="+URLEncoder.encode(query, "US-ASCII")+"&lt="+localTime+"&to="+timeOffset);
HttpResponse response = httpclient.execute(httpget);
System.out.println("Status"+ response.getStatusLine());
System.out.println("Length "+org.apache.http.util.EntityUtils.toString(response.getEntity()).length());
return org.apache.http.util.EntityUtils.toString(response.getEntity());

Catlog :

I/System.out(22344): StatusHTTP/1.1 200 OK
I/System.out(21737): Length 0

The uri is good,when I copy/past it in a browser I have a result like this :

{"ads":[{"i"...],"pxs":{}}

Does anyone have an idea why my string is empty?

Udpate 1:

    long localTime = System.currentTimeMillis();
    Date date = new Date();
    @SuppressWarnings("deprecation")
    int timeOffset = date.getTimezoneOffset ();

Update 2:

I added this line :

httpget.setHeader("User-Agent", "Mozilla/5.0 (Windows; U; MSIE 9.0; Windows NT 9.0; en-US)");

And now I have this error :

Catlog

 E/AndroidRuntime(23584): FATAL EXCEPTION: AsyncTask #2
 E/AndroidRuntime(23584): java.lang.RuntimeException: An error occured while executing doInBackground()
 E/AndroidRuntime(23584):   at android.os.AsyncTask$3.done(AsyncTask.java:278)
 E/AndroidRuntime(23584):   at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
 E/AndroidRuntime(23584):   at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
 E/AndroidRuntime(23584):   at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
 E/AndroidRuntime(23584):   at java.util.concurrent.FutureTask.run(FutureTask.java:137)
 E/AndroidRuntime(23584):   at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
 E/AndroidRuntime(23584):   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
 E/AndroidRuntime(23584):   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
 E/AndroidRuntime(23584):   at java.lang.Thread.run(Thread.java:864)
 E/AndroidRuntime(23584): Caused by: java.lang.IllegalStateException: Content has been consumed
 E/AndroidRuntime(23584):   at org.apache.http.entity.BasicHttpEntity.getContent(BasicHttpEntity.java:84)
like image 817
GermainGum Avatar asked Oct 22 '22 21:10

GermainGum


1 Answers

System.out.println("Length "+org.apache.http.util.EntityUtils.toString(response.getEntity()).length());
return org.apache.http.util.EntityUtils.toString(response.getEntity());

You are using it twice. Thats why you get

java.lang.IllegalStateException: Content has been consumed

try setting it to a reference.

            HttpEntity entity = response.getEntity();
            String responseText = EntityUtils.toString(entity);

then do what you have to do with responseText.

like image 189
wtsang02 Avatar answered Oct 27 '22 09:10

wtsang02