Android InputStreamReader在完成前停止
我正在进行Android开发,我正在使用HttpURLConnection、InputStream和InputStreamReader。我正在使用android studio,在InputStreamReader上遇到了一个问题。我试图简单地从一个网站下载所有html代码,并将其显示在日志中,以便稍后提取字符串。它只是停止下载。我在互联网上搜索了一个解决方案,但我无法找到答案。任何帮助都会很好 这是密码 这是日志Android InputStreamReader在完成前停止,android,android-asynctask,inputstream,httpurlconnection,Android,Android Asynctask,Inputstream,Httpurlconnection,我正在进行Android开发,我正在使用HttpURLConnection、InputStream和InputStreamReader。我正在使用android studio,在InputStreamReader上遇到了一个问题。我试图简单地从一个网站下载所有html代码,并将其显示在日志中,以便稍后提取字符串。它只是停止下载。我在互联网上搜索了一个解决方案,但我无法找到答案。任何帮助都会很好 这是密码 这是日志 我使用BufferedReader读取输入流。我使用high timeout,因为
我使用BufferedReader读取输入流。我使用high timeout,因为服务器有时需要将近50秒来运行一些测试并生成html响应
try{
URL url = new URL(mServerAddress);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setReadTimeout(60000);
urlConnection.setConnectTimeout(5000);
InputStream response = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader responseReader = new BufferedReader(new InputStreamReader(response, "UTF-8"));
StringBuilder total = new StringBuilder();
String line;
while((line = responseReader.readLine()) != null){
total.append(line);
}
response.close();
}catch(Exception e){
e.printStackTrace();
}
您可以使用System.out.printlnline;在while循环中,您是否尝试过增加超时,尤其是读取超时?示例urlConnection.setReadTimeout30000;?在我最初寻找答案的过程中,我没有遇到这个问题。我刚刚发现了很多关于套接字超时的引用,但我不认为我正在使用套接字。我该怎么做?urlConnection.setConnectTimeout5000;。Http请求位于应用层,要连接到服务器,需要使用网络套接字的传输TCP、UDP、SCTP。您正在间接地使用套接字。尝试添加这两个语句,但两次似乎都在同一个位置结束。这是它派生的html的最后一行:10-16 23:45:46.557 27174-27174/com.example.th.guesstheanimali/Contents of URL:我猜不是超时。我将粘贴用于执行类似操作的代码。区别在于我使用缓冲流读取器并将其保存到文件中。你对Android使用这种方法吗?是的,@tanner。安卓果冻豆
10-16 22:22:55.413 11287-11287/? I/art: Not late-enabling -Xcheck:jni (already on)
10-16 22:22:55.846 11287-11299/com.example.th.guesstheanimal I/art: Background sticky concurrent mark sweep GC freed 3060(251KB) AllocSpace objects, 0(0B) LOS objects, 29% free, 797KB/1135KB, paused 9.623ms total 33.103ms
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: <html xmlns="http://www.w3.org/1999/xhtml" class="new-nav">
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: <head>
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: <title>Top 25 Celebrity Hot List : People.com</title>
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: <link rel="shortcut icon" href="http://img2.timeinc.net/people/favicon.ico" />
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: <meta name="robots" content="noarchive" />
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: <link rel="stylesheet" type="text/css" href="/people/static/c/main.css" media="all" />
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: <link rel="stylesheet" type="text/css" href="/people/static/c/package/top25celebrityhotlist/main.css" media="all" />
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: <link rel="alternate" type="application/rss+xml" title="People.com - Top Headlines [RSS]" href="http://rss.people.com/web/people/rss/topheadlines/index.xml" />
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: <script type="text/javascript" language="javascript" src="http://ar.atwola.com/file/adsWrapper.js"></script>
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: <script type="text/javascript" language="javascript" src="http://tiads.people.com/ads/tgx.js"></script>
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: <script type="text/javascript" language="javascript" src="/people/js/0,,,00.js"></script>
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: <script type="text/javascript" language="javascript" src="/people/js/browserDetection/0,,,00.js"></script>
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: <script type="text/javascript" language="javascript" src="http://tiads.people.com/ads/tgx.js"></script>
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: <script type="text/javascript" language="javascript">
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: var adFactory = new TiiAdFactory(adConfig, "celebrity/top25");
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: adFactory.setArticleId("");
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: adFactory.setPackageId("");
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: adFactory.setChannel("celebrity");
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: adFactory.setSubchannel("dietcoke");
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: adFactory.setContentPage();
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: adFactory.setContentType("package");
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: adFactory.setParam("page", "");
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: adFactory.setParam("franc", "");
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: adFactory.setParam("subj", new Array(""));
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: adFactory.setParam("celeb", new Array(""));
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: adFactory.setParam("sourc", source);
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: </script>
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: </head>
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: <body id="mostbeautiful" class="article">
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: <!-- /h/inc/social/async-social.txt -->
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: <div id="fb-root">
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: <script type="text/javascript" language="javascript">
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: window.fbAsyncInit = function() {
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: FB.init({appId: '56579077293', status:true, cookie: true, xfbml: true, oauth : true});
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: };
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: </script>
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: </div>
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: <!-- async load facebook, google plus one, twitter -->
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: <script type="text/javascript">
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: (function(d, s) {
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: var js,
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: fjs = d.getElementsByTagName(s)[0],
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: load = function(url, id) {
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: if (d.getElementById(id)) {return;}
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: js = d.createElement(s);
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: js.src = url;
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: js.id = id;
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: fjs.parentNode.insertBefore(js, fjs);
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: };
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: load('//connect.facebook.net/en_US/all.js', 'fb-all-js');
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: load('https://apis.google.com/js/plusone.js', 'google-plus1-js');
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: load('//platform.twitter.com/widgets.js', 'tweet-js');
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: }(document, 'script'));
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: </script>
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: <div id="omniture" style="display:none">
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: <script language="JavaScript">
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: <!-- // Hide
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: var s_account="timepeople";
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: // -->
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: </script>
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: <script language="JavaScript" src="http://img.timeinc.net/tii/omniture/h/common.js"></script>
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: <script language="JavaScript" src="http://img.timeinc.net/tii/omniture/h/config/people.js"></script>
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: <script language="JavaScript">
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: <!-- // Hide
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL:
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: s_time.channel = 'people';
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: s_time.pageName = 'people|celebrity|top 25|page 1';
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: s_time.prop11 = 'special feature';
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: s_time.prop12 = 'top 25 celebrity hot list';
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: s_time.prop16 = 'celebrity';
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL:
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: s_time.prop17 = location.href;
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL:
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL:
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: if (typeof(catsCSV) == "string") s_time.prop13 = catsCSV;
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: if (typeof(omnitureHookFunction) == "function") eval("omnitureHookFunction();");
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: var s_code=s_time.t();if(s_code)document.write(s_code)//-->
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: </script>
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: </div>
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: <div id="container">
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: <!-- .html extension -->
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: <div id="insider-nav">
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: <div class="main">
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: <div class="left">
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: <a href="/people/" class="site active" id="people-com"><span>People.com</span></a>
10-16 22:22:56.395 11287-11287/com.example.th.guesstheanimal I/Contents of URL: <a href="/people/insider/" class="site" id="people-premium"><span>
10-16 22:22:56.407 11287-11328/com.example.th.guesstheanimal D/OpenGLRenderer: Render dirty regions requested: true
10-16 22:22:56.408 11287-11287/com.example.th.guesstheanimal D/: HostConnection::get() New Host Connection established 0xa6680c90, tid 11287
10-16 22:22:56.418 11287-11287/com.example.th.guesstheanimal D/Atlas: Validating map...
10-16 22:22:56.486 11287-11328/com.example.th.guesstheanimal D/: HostConnection::get() New Host Connection established 0xa6680f40, tid 11328
10-16 22:22:56.498 11287-11328/com.example.th.guesstheanimal I/OpenGLRenderer: Initialized EGL, version 1.4
10-16 22:22:56.507 11287-11328/com.example.th.guesstheanimal D/OpenGLRenderer: Enabling debug mode 0
10-16 22:22:56.522 11287-11328/com.example.th.guesstheanimal W/EGL_emulation: eglSurfaceAttrib not implemented
10-16 22:22:56.522 11287-11328/com.example.th.guesstheanimal W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa638a1a0, error=EGL_SUCCESS
10-16 22:24:34.810 11287-11294/com.example.th.guesstheanimal W/art: Suspending all threads took: 5.688ms
try{
URL url = new URL(mServerAddress);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setReadTimeout(60000);
urlConnection.setConnectTimeout(5000);
InputStream response = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader responseReader = new BufferedReader(new InputStreamReader(response, "UTF-8"));
StringBuilder total = new StringBuilder();
String line;
while((line = responseReader.readLine()) != null){
total.append(line);
}
response.close();
}catch(Exception e){
e.printStackTrace();
}