HTTP GET 请求后,结果字符串被截断 - 内容已被消耗

发布于 2024-08-30 12:08:39 字数 3188 浏览 10 评论 0原文

我正在发出这样的 http get 请求:

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 (...) {
    ...
}

一切正常...唯一的问题:Log.i 的输出被截断...这不是完整的 html 页面。如果我在浏览器中发出相同的请求,我会得到 3 倍的输出,这与在模拟器中发出请求并使用上面的代码相反......出了什么问题?

错误:

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)

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>0=Aachen&T0=H&HT0="+start_from+">1=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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

落日海湾 2024-09-06 12:08:39

好的,我解决了。
据我了解,如果不“使用”实体的内容,就会被消耗。因此,我使用 Entity 的内容创建了一个新的 String 对象,也就是说,它是一个副本,而不是对实体内容的引用。

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>0=Aachen&T0=H&HT0="+start_from+">1=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(...) {
...
}

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>0=Aachen&T0=H&HT0="+start_from+">1=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(...) {
...
}
相守太难 2024-09-06 12:08:39

DixieFlatline 是正确的。

有了这个,我得到了部分输出:

Log.v(TAG, "RESPONSE: " + response);

有了这个,我得到了完整的输出:

for(String line : response.split("\n"))
   Log.v(TAG, "LINE: " + line);

DixieFlatline is correct.

With this I get partial output:

Log.v(TAG, "RESPONSE: " + response);

With this I get full output:

for(String line : response.split("\n"))
   Log.v(TAG, "LINE: " + line);
物价感观 2024-09-06 12:08:39

我认为 Log 将字符数限制为 4070。我尝试在 ddms locgat 中显示长字符串,但只得到 4070 个字符。

I think that Log limits the number of chars to 4070. I tried to display a long string in ddms locgat and i got only 4070 chars.

梦情居士 2024-09-06 12:08:39

伟大的!

顺便说一句,如果您使用带有调试视角的 Eclipse,则在“变量”视图中:(

窗口 -> 显示视图 -> 变量)

您可以右键单击窗口,选择“最大长度”,然后插入“0”。

这样您就可以在变量视图中看到所有字符(完整的 html)。

Great!

By the way, if you are using Eclipse with Debug perspective, at the "Variables" view:

(Window -> Show View -> Variables)

you can right-click on the window, select "Max Length" and insert "0".

This way you can see all the chars (the complete html) in the Variables view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文