1

我的目标是制作一个非常基本的应用程序,它读取 HTML 并将其存储到字符串中。我只对网站源代码中的一行感兴趣。我发现了一个建议这个话题:

String bodyHtml = "null";
            try {
                String myUri = "http://www.spring8.or.jp/ext/ja/status/text.html";
                HttpClient httpClient = new DefaultHttpClient();
                HttpGet get = new HttpGet(myUri);

                HttpResponse response = httpClient.execute(get);

                // Build up result
                bodyHtml = EntityUtils.toString(response.getEntity());
            } catch (Exception e) {

            }

            url.setText(bodyHtml);

url 是我的文本视图。据我所知,我已正确设置清单中的权限。

但是,当我在手机和模拟器上运行此代码时,它似乎根本不起作用。我什么都得不到。我错过了什么吗?

谢谢

4

3 回答 3

0

Try this,

Call the below method to download the HTml Content and pass the Url in the parameter,

private void downloadText(String urlStr) {
        progressDialog = ProgressDialog.show(this, "", 
                "Download Text from " + urlStr);
        final String url = urlStr;

        new Thread () {
            public void run() {
                int BUFFER_SIZE = 2000;
                InputStream in = null;
                Message msg = Message.obtain();
                msg.what=1;
                try {
                    in = openHttpConnection(url);

                    InputStreamReader isr = new InputStreamReader(in);
                    int charRead;
                      text = "";
                      char[] inputBuffer = new char[BUFFER_SIZE];

                          while ((charRead = isr.read(inputBuffer))>0)
                          {                    
                              String readString = 
                                  String.copyValueOf(inputBuffer, 0, charRead);                    
                              text += readString;
                              inputBuffer = new char[BUFFER_SIZE];
                          }
                         Bundle b = new Bundle();
                            b.putString("text", text);
                            msg.setData(b);
                          in.close();

                }catch (IOException e2) {
                    e2.printStackTrace();
                }
                messageHandler.sendMessage(msg);
            }
        }.start();    
    }

This the helper method which returns InputStream Object,

private InputStream openHttpConnection(String urlStr) {
    InputStream in = null;
    int resCode = -1;

    try {
        URL url = new URL(urlStr);
        URLConnection urlConn = url.openConnection();

        if (!(urlConn instanceof HttpURLConnection)) {
            throw new IOException ("URL is not an Http URL");
        }

        HttpURLConnection httpConn = (HttpURLConnection)urlConn;
        httpConn.setAllowUserInteraction(false);
    httpConn.setInstanceFollowRedirects(true);
    httpConn.setRequestMethod("GET");
    httpConn.connect(); 

    resCode = httpConn.getResponseCode();                 
    if (resCode == HttpURLConnection.HTTP_OK) {
        in = httpConn.getInputStream();                                 
    }         
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return in;
}

And now display the String in a textView using Handler,

private Handler messageHandler = new Handler() {

        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {

            case 1:
                TextView text = (TextView) findViewById(R.id.textview01);
                text.setText(msg.getData().getString("text"));
                break;
            }
            progressDialog.dismiss();
        }
    };

Provide the INTERNET permission in the manifest.

于 2012-06-28T08:54:17.897 回答
0

试试这个而不是 EntityUtils

BufferedReader rd = new BufferedReader(new InputStreamReader(
        response.getEntity().getContent()));
String line = "";
String newLine = "";
while ((line = rd.readLine()) != null) {
    newLine = newLine.concat(line);
}
System.out.println(newLine);
于 2012-06-28T08:50:41.030 回答
0

在 的执行方法中HttpClient,也放一个HttpContext如下所示:

HttpContext localContext = new BasicHttpContext();
HttpResponse response = httpClient.execute(get, localContext);

并且还使用BufferedReader

final BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

如果它不起作用,您可能会遇到 Internet 连接问题。顺便说一句,不要忘记 android.permission.INTERNET 权限!

于 2012-06-28T08:51:13.380 回答