获取网页文字

时间:2015-01-31 16:44:27

标签: java android

我正在尝试编写我的第一个Android应用程序,需要将网页的文本作为字符串显示在TextView中。我在StackOverflow上找到了一些示例,但由于某些原因,它们似乎都不适用于我。当我单击按钮以检索文本时,应用程序崩溃。这是我现在所拥有的(基于Get text from web page to string的代码):

MainActivity.java文件



    public class MainActivity extends ActionBarActivity {

      Button testbutton;
      Button btnReset;
      TextView serverMsgViewComponent;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          addListenerOnButton();
      }

      public void addListenerOnButton() {
          testbutton = (Button) findViewById(R.id.btnClickme);
          btnReset = (Button) findViewById(R.id.btnResetText);
          serverMsgViewComponent = (TextView) findViewById(R.id.serverMsgView);
          serverMsgViewComponent.setText("Custom text");
          final ReadWebpageAsyncTask readpage = new ReadWebpageAsyncTask();
          btnReset.setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View arg0) {
                  serverMsgViewComponent.setText("Server message placeholder");
              }
          });
          testbutton.setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View arg0) {
                  serverMsgViewComponent.setText("Retrieving message from server...");
                  readpage.readWebpage();
              }

          });

      }

      //some default code here
    }

ReadWebpageAsyncTask.java

public class ReadWebpageAsyncTask extends Activity { private TextView textView; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.serverMsgView); } private class DownloadWebPageTask extends AsyncTask<String, Void, String> { @Override protected String doInBackground(String... urls) { String response = ""; for (String url : urls) { DefaultHttpClient client = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(url); try { HttpResponse execute = client.execute(httpGet); InputStream content = execute.getEntity().getContent(); BufferedReader buffer = new BufferedReader( new InputStreamReader(content)); String s = ""; while ((s = buffer.readLine()) != null) { response += s; } } catch (Exception e) { e.printStackTrace(); } } return response; } @Override protected void onPostExecute(String result) { textView.setText(Html.fromHtml(result)); } } public void readWebpage() { DownloadWebPageTask task = new DownloadWebPageTask(); task.execute(new String[] { "http://hglstudio.com/workspace/server.htm" }); } }
public class ReadWebpageAsyncTask extends Activity { private TextView textView; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.serverMsgView); } private class DownloadWebPageTask extends AsyncTask<String, Void, String> { @Override protected String doInBackground(String... urls) { String response = ""; for (String url : urls) { DefaultHttpClient client = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(url); try { HttpResponse execute = client.execute(httpGet); InputStream content = execute.getEntity().getContent(); BufferedReader buffer = new BufferedReader( new InputStreamReader(content)); String s = ""; while ((s = buffer.readLine()) != null) { response += s; } } catch (Exception e) { e.printStackTrace(); } } return response; } @Override protected void onPostExecute(String result) { textView.setText(Html.fromHtml(result)); } } public void readWebpage() { DownloadWebPageTask task = new DownloadWebPageTask(); task.execute(new String[] { "http://hglstudio.com/workspace/server.htm" }); } }

1 个答案:

答案 0 :(得分:1)

所以,我说我从另一个SO讨论中复制了代码,这段代码对我不起作用,我需要帮助找出原因。相反,我把我的问题标记为另一个我从中获取代码的副本,我的问题也被低估了。谢谢,StackOverflow!

现在,我自己发现了这个问题(很开心!)。问题在于域名由于某种原因未能解析为IP地址,然后引发了错误。解决方案是首先通过访问URL一次“初始化”域,然后尝试在第二次尝试时下载文本。

所以我先调用这个函数:

private void initializeDns(String url) {
    try {
        InetAddress address = InetAddress.getByName(url);
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }
}