2013-01-22 53 views
0

我很新到Android编程阅读的文本,但我已经习惯了编程的iOS虽然...的Android从网站

现在的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) 
+1

显示Logcat错误会很有帮助。但在黑暗中刺中,你有没有在你的Manifest文件中声明Internet权限? '<使用权限android:name =“android.permission.INTERNET”/>' –

+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。详情请看AsyncTaskAsyncTask的接口是一个小傻瓜恕我直言。

AsyncTask没有什么特别之处,它围绕创建线程和根据结果更新用户界面的方式进行包装。在过去,我们曾经这样做,

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

为我工作过,谢谢! –

0

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

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

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