0

我对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)
4

2 回答 2

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();
于 2013-01-22T18:40:04.000 回答
0

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

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

请记住,您不能doInBackground()从该方法访问 UI 线程。

于 2013-03-03T17:39:54.903 回答