Android从网站上读取文本

时间:2013-01-22 16:50:59

标签: android string url

我对Android编程很陌生,但我习惯为iOS编程......

现在在iOS中你有NSString stringWithContentOfURL的功能。现在我的问题是,如果java中有类似的选项......?虽然在谷歌搜索我已经通过一些例子,但使用它们我的应用程序总是崩溃......

我使用了这样的代码:http://www.coderzheaven.com/2011/07/17/how-to-read-webpage-contents-as-a-string-in-android/但它一直在崩溃......你能帮帮我吗?

谢谢!

编辑: LogCat错误:

01-22 19:34:12.718: E/AndroidRuntime(5481): FATAL EXCEPTION: main
01-22 19:34:12.718: E/AndroidRuntime(5481): android.os.NetworkOnMainThreadException
01-22 19:34:12.718: E/AndroidRuntime(5481): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
01-22 19:34:12.718: E/AndroidRuntime(5481): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
01-22 19:34:12.718: E/AndroidRuntime(5481): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
01-22 19:34:12.718: E/AndroidRuntime(5481): at java.net.InetAddress.getAllByName(InetAddress.java:220)
01-22 19:34:12.718: E/AndroidRuntime(5481):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
01-22 19:34:12.718: E/AndroidRuntime(5481):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
01-22 19:34:12.718: E/AndroidRuntime(5481):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
01-22 19:34:12.718: E/AndroidRuntime(5481):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
01-22 19:34:12.718: E/AndroidRuntime(5481):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
01-22 19:34:12.718: E/AndroidRuntime(5481):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:653)
01-22 19:34:12.718: E/AndroidRuntime(5481):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627)
01-22 19:34:12.718: E/AndroidRuntime(5481):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:616)
01-22 19:34:12.718: E/AndroidRuntime(5481):     at com.example.test4.DieActivity.meldeAn(DieActivity.java:150)
01-22 19:34:12.718: E/AndroidRuntime(5481):     at com.example.test4.DieActivity$1.onClick(DieActivity.java:56)
01-22 19:34:12.718: E/AndroidRuntime(5481):     at android.view.View.performClick(View.java:3511)
01-22 19:34:12.718: E/AndroidRuntime(5481):     at android.view.View$PerformClick.run(View.java:14105)
01-22 19:34:12.718: E/AndroidRuntime(5481):     at android.os.Handler.handleCallback(Handler.java:605)
01-22 19:34:12.718: E/AndroidRuntime(5481):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-22 19:34:12.718: E/AndroidRuntime(5481):     at android.os.Looper.loop(Looper.java:137)
01-22 19:34:12.718: E/AndroidRuntime(5481):     at android.app.ActivityThread.main(ActivityThread.java:4424)
01-22 19:34:12.718: E/AndroidRuntime(5481):     at java.lang.reflect.Method.invokeNative(Native Method)
01-22 19:34:12.718: E/AndroidRuntime(5481):     at java.lang.reflect.Method.invoke(Method.java:511)
01-22 19:34:12.718: E/AndroidRuntime(5481):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-22 19:34:12.718: E/AndroidRuntime(5481):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-22 19:34:12.718: E/AndroidRuntime(5481):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:2)

您已尝试在UI线程上执行(网络)IO。这是不允许的。为什么?因为如果您挂起UI线程,系统会将ANR(活动未响应)对话框发布给用户..

您必须异步执行IO,

new AysyncTask<String,Void,String>() {
            protected String doInBackground(String... urls) {
              String url = urls[0];
              // do network operation, get result in a string (for example)
              return resultString;
            }

            protected void onPostExecute(String result) {
              JSONObject jo = new JSONObject(result);
              // update UI with the results of the network call here
            }
        }.execute(aUrl);

这对于网络IO尤其重要,但它也适用于磁盘IO。请查看AsyncTask了解详情。 AsyncTask的界面有点迟钝恕我直言。

AsyncTask并没有什么特别之处,它围绕创建线程并根据结果更新用户界面包装了一个模式。在过去,我们过去常常这样做,

new Thread(new Runnable() { 
  @Override
  public void run() {
    // do network operation
    runOnUiThread(new Runnable() {
      @Override
      public void run() {
        // update UI thread here
      }
    });
  }
}}.start();

答案 1 :(得分:0)

Android告诉您不应该在UI线程上进行网络操作。它不希望你阻止线程,并且有充分的理由。

尝试使用AsyncTask代替。将您的HTTP请求代码放在doInBackground()方法中,并使用TextView方法更新onPostExecute()

请注意, > doInBackground()方法访问UI线程。