HTTP GET 请求后,结果字符串被截断 - 内容已被消耗
我正在发出这样的 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>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 (...) {
...
}
一切正常...唯一的问题: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好的,我解决了。
据我了解,如果不“使用”实体的内容,就会被消耗。因此,我使用 Entity 的内容创建了一个新的 String 对象,也就是说,它是一个副本,而不是对实体内容的引用。
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.
DixieFlatline 是正确的。
有了这个,我得到了部分输出:
有了这个,我得到了完整的输出:
DixieFlatline is correct.
With this I get partial output:
With this I get full output:
我认为 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.
伟大的!
顺便说一句,如果您使用带有调试视角的 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.