InputStreamReader在完成前停止

时间:2015-10-17 19:08:13

标签: android android-asynctask inputstream httpurlconnection

我正在开发Android开发,我正在使用HttpURLConnection,InputStream和InputStreamReader。我正在使用android studio并且遇到了InputStreamReader的问题。我试图简单地从网站上下载所有的html代码并将其显示在日志中,以便稍后提取字符串。它只是停止下载。我在互联网上搜索了一个解决方案,我无法弄明白。任何帮助都会很棒!

这是代码

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    DownloadTask task = new DownloadTask();
    String result = null;

    try {
        result = task.execute("http://www.people.com/people/static/h/package/top25celebrityhotlist/").get(); //http://list25.com/25-most-popular-animals-on-google-search/
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }

    Log.i("Contents of URL", result);
}


public class DownloadTask extends AsyncTask<String, Void, String>{
    //First String is content to be passed, the Void is the method to be run, and the second String is the returned content


    @Override
    protected String doInBackground(String... urls) { //varargs not quite an array
        StringBuilder total = new StringBuilder();
        URL url;
        HttpURLConnection urlConnection = null;

        try {
            url = new URL(urls[0]);
            urlConnection = (HttpURLConnection)url.openConnection();
            InputStream in = urlConnection.getInputStream();
            InputStreamReader reader = new InputStreamReader(in);

            int data = reader.read();

            while(data != -1){
                char current = (char) data;
                total.append(current);

                data = reader.read();
            }
            return total.toString();
        }
        catch (Exception e) {
            e.printStackTrace();
            return "Failed";

        }

    }
}

这是日志

    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

1 个答案:

答案 0 :(得分:0)

我使用BufferedReader来读取输入流。我使用高超时,因为服务器有时需要将近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();
}

您可以在while循环中使用System.out.println(line);来查看它是用logcat编写的