首先,这段代码是我在Android中用程序直接读取百度百科的html页面,然后再到程序中解析,现在的问题是,它还没开始读,就已经报错了!
情况如下:
这是报错信息:
07-05 23:31:36.827: W/System.err(2847): java.io.EOFException
07-05 23:31:36.835: W/System.err(2847): at java.util.zip.GZIPInputStream.readFully(GZIPInputStream.java:214)
07-05 23:31:36.835: W/System.err(2847): at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:81)
07-05 23:31:36.835: W/System.err(2847): at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:64)
07-05 23:31:36.842: W/System.err(2847): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.initContentStream(HttpURLConnectionImpl.java:539)
07-05 23:31:36.842: W/System.err(2847): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.retrieveResponse(HttpURLConnectionImpl.java:1046)
07-05 23:31:36.842: W/System.err(2847): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:512)
07-05 23:31:36.842: W/System.err(2847): at com.avin.priney.utils.GBKGetter.getKnowledgeByWord(GBKGetter.java:378)
07-05 23:31:36.842: W/System.err(2847): at com.avin.priney.android.AnswerStrategy.getFinalAnswer(AnswerStrategy.java:265)
07-05 23:31:36.842: W/System.err(2847): at com.avin.priney.android.PrineyMainActivity.getResultTxtByQuestionTxt(PrineyMainActivity.java:272)
07-05 23:31:36.850: W/System.err(2847): at com.avin.priney.android.PrineyMainActivity.onClick(PrineyMainActivity.java:145)
07-05 23:31:36.850: W/System.err(2847): at android.view.View.performClick(View.java:2485)
07-05 23:31:36.850: W/System.err(2847): at android.view.View$PerformClick.run(View.java:9090)
07-05 23:31:36.850: W/System.err(2847): at android.os.Handler.handleCallback(Handler.java:587)
07-05 23:31:36.850: W/System.err(2847): at android.os.Handler.dispatchMessage(Handler.java:92)
07-05 23:31:36.850: W/System.err(2847): at android.os.Looper.loop(Looper.java:130)
07-05 23:31:36.858: W/System.err(2847): at android.app.ActivityThread.main(ActivityThread.java:3859)
07-05 23:31:36.858: W/System.err(2847): at java.lang.reflect.Method.invokeNative(Native Method)
07-05 23:31:36.858: W/System.err(2847): at java.lang.reflect.Method.invoke(Method.java:507)
07-05 23:31:36.858: W/System.err(2847): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-05 23:31:36.858: W/System.err(2847): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-05 23:31:36.858: W/System.err(2847): at dalvik.system.NativeStart.main(Native Method)
下面贴出来出问题的代码段:
String webUrl = "http://baike.baidu.com/searchword/?word="+nCode+"&pic=1";
//内置的百科页面地址
String contentUrl = "http://baike.baidu.com";
BufferedReader br = null;
String backLine = null;
URL url = null;
try {
url = new URL(webUrl);
Log.d("Avin","webUrl going open...");
URLConnection conn = url.openConnection();
Log.d("Avin","webUrl opened...");
Log.d("Avin","the question is-->"+question);
//异常信息显示是这一行报错
br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
backLine = br.readLine();
Log.d("Avin",backLine);
int clmindex = 1;
while(backLine != null){
backLine = br.readLine();
/*
if(clmindex == 20){
//跟踪到目标页面所在行,跳出循环
break;
}
*/
System.out.println(clmindex+"-->"+backLine);
clmindex++;
}
br.close();
br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
这一行报错!查了一天的资料,就是没进展,
这个异常意思是读取的文件异常结束了,返回的错误状态,但是看报错的代码行此时还没有开始读数据呢,怎么会异常结束呢,百思不得其解!
这段代码放在JavaProject中就没事,到我的手机上就不行!
走投无路,特此来等高手解答!
------解决方案--------------------
关注,刚刚也遇到了这个问题,好像是conn.getInputStream()出错了
------解决方案--------------------
我的程序好像也是buffer = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));,根本就没执行,就直接到catch了
------解决方案--------------------
我在stackoverflow上看到,有人是这样解的
System.setProperty("http.keepAlive", "false");
而且我们也验证过了,确实好用。
------解决方案--------------------
我也遇到了 唉。。。