Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
335 views
in Technique[技术] by (71.8m points)

java - After HTTP GET request, the resulting string is cut-off - content has been consumed

I'm making a http get request like this:

try {
    HttpClient client = new DefaultHttpClient();  
    String getURL = "http://busspur02.aseag.de/bs.exe?SID=5FC39&ScreenX=1440&ScreenY=900&CMD=CR&Karten=true&DatumT="+day+"&DatumM="+month+"&DatumJ="+year+"&ZeitH="+hour+"&ZeitM="+min+"&Intervall=60&Suchen=(S)uchen&GT0=Aachen&T0=H&HT0="+start_from+"&GT1=Aachen&T0=H&HT1="+destination+"";
    HttpGet get = new HttpGet(getURL);
    HttpResponse responseGet = client.execute(get);  
    HttpEntity resEntityGet = responseGet.getEntity();

    if (resEntityGet != null) {  
        //do something with the response
        Log.i("GET RESPONSE",EntityUtils.toString(resEntityGet));
    } 
    ...
} catch (...) {
    ...
}

It all works well... the only problem: the output from Log.i is cut-off... It's not the complete html page. If I make the same request in a browser, I get 3x the output in opposition to making the request in the emulator and using the above code.... what's wrong?

ERROR:

04-30 14:01:01.287: WARN/System.err(1088): java.lang.IllegalStateException: Content has been consumed
04-30 14:01:01.297: WARN/System.err(1088):     at org.apache.http.entity.BasicHttpEntity.getContent(BasicHttpEntity.java:84)
04-30 14:01:01.297: WARN/System.err(1088):     at org.apache.http.conn.BasicManagedEntity.getContent(BasicManagedEntity.java:100)
04-30 14:01:01.307: WARN/System.err(1088):     at org.apache.http.util.EntityUtils.toString(EntityUtils.java:112)
04-30 14:01:01.307: WARN/System.err(1088):     at org.apache.http.util.EntityUtils.toString(EntityUtils.java:146)
04-30 14:01:01.307: WARN/System.err(1088):     at mjb.project.AVV.ParseHTML.start(ParseHTML.java:177)
04-30 14:01:01.307: WARN/System.err(1088):     at mjb.project.AVV.ParseHTML.onCreate(ParseHTML.java:139)
04-30 14:01:01.307: WARN/System.err(1088):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-30 14:01:01.327: WARN/System.err(1088):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
04-30 14:01:01.327: WARN/System.err(1088):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
04-30 14:01:01.327: WARN/System.err(1088):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
04-30 14:01:01.347: WARN/System.err(1088):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
04-30 14:01:01.347: WARN/System.err(1088):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-30 14:01:01.347: WARN/System.err(1088):     at android.os.Looper.loop(Looper.java:123)
04-30 14:01:01.347: WARN/System.err(1088):     at android.app.ActivityThread.main(ActivityThread.java:4363)
04-30 14:01:01.347: WARN/System.err(1088):     at java.lang.reflect.Method.invokeNative(Native Method)
04-30 14:01:01.357: WARN/System.err(1088):     at java.lang.reflect.Method.invoke(Method.java:521)
04-30 14:01:01.357: WARN/System.err(1088):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
04-30 14:01:01.357: WARN/System.err(1088):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-30 14:01:01.357: WARN/System.err(1088):     at dalvik.system.NativeStart.main(Native Method)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

OK I solved it.
As I understand, the content of the entity gets consumed if it's not "used". So I created a new String object with the content of the Entity, so to say a copy instead of a reference to the entity's content.

try {
    HttpClient client = new DefaultHttpClient();  
    String getURL = "http://busspur02.aseag.de/bs.exe?SID=5FC39&ScreenX=1440&ScreenY=900&CMD=CR&DatumT="+day+"&DatumM="+month+"&DatumJ="+year+"&ZeitH="+hour+"&ZeitM="+min+"&Intervall=120&Suchen=(S)uchen&GT0=Aachen&T0=H&HT0="+start_from+"&GT1=Aachen&T0=H&HT1="+destination+"";
    HttpGet get = new HttpGet(getURL);
    HttpResponse responseGet = client.execute(get);  
    HttpEntity resEntityGet = responseGet.getEntity();  
    String sourceString = "";

    if (resEntityGet != null) {                 
        try {
            dismissDialog();
            sourceString= new String(EntityUtils.toString(resEntityGet));
            Log.i("GET RESPONSE", sourceString);
        } catch (ParseException exc) {
            // TODO Auto-generated catch block
            exc.printStackTrace();
        } catch (IllegalStateException exc) {
            // TODO Auto-generated catch block
            exc.printStackTrace();
        }
        ...
    }
    ...
} catch(...) {
...
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...