尝试返回所有URL http内容时,内容会中断

时间:2018-12-19 18:54:24

标签: android-studio android-asynctask inputstream httpurlconnection inputstreamreader

我正在尝试从网站返回http内容。但是,并非所有内容都可以显示。

我正在使用Android Studio 3.1.4,SDK版本27。

作为参考,这是在线Udemy课程:完整的Android N开发人员课程中练习的一部分,在该课程中,我通过从列出并显示“热门”名人的网站上获取url内容来制作Guess The Celebrity应用程序。使用AsyncTask获取内容后,我将使用正则表达式在内容中搜索名人姓名和图片。

MainActivity.java:

public class MainActivity extends AppCompatActivity {

public class DownloadTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {
        String result = "";
        URL url;
        HttpURLConnection urlConnection = null;

        try {
            url = new URL(urls[0]);
            urlConnection = (HttpURLConnection) url.openConnection();
            InputStream inputStream = urlConnection.getInputStream();
            InputStreamReader reader = new InputStreamReader(inputStream);
            int data = reader.read();
            while (data != -1) {
                char current = (char) data;
                result += current;
                data = reader.read();
            }
            return result;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

@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.posh24.se/kandisar").get();

        Log.i("URL CONTENTS ", result);
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }
}

我正在尝试返回url http内容的全部。但是,我只得到一个片段。以下是http内容切断后显示的内容:

....
<div class="articleContainer contentBlock ">
    <h1 class="header">Topp 100 kändisar</h1>   
    <div class="channelList">
        <div class="channels_nav">
    <div class="title">Lista:</div>
    <div class=

D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
D/: HostConnection::get() New Host Connection established 0xab6833c0, tid 9204
D/android.support.v7.widget.GridLayout: vertical constraints: y5-y0>=680, y5-y4<=72, y4-y3<=72, y3-y2<=72, y2-y1<=72, y1-y0<=162 are inconsistent; permanently removing: y5-y4<=72. 
D/: HostConnection::get() New Host Connection established 0xaddbb480, tid 9274
I/OpenGLRenderer: Initialized EGL, version 1.4
W/OpenGLRenderer: Failed to choose config with         
EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
D/EGL_emulation: eglCreateContext: 0xa3ef2600: maj 3 min 0 rcv 3
D/EGL_emulation: eglMakeCurrent: 0xa3ef2600: ver 3 0 (tinfo 0xab6b2980)
E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf
                  glUtilsParamSize: unknow param 0x00008824
D/EGL_emulation: eglMakeCurrent: 0xa3ef2600: ver 3 0 (tinfo 0xab6b2980)
W/art: Before Android 4.1, method int android.support.v7.widget.DropDownListView.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView

0 个答案:

没有答案