2014-04-22 41 views
-1

我想要掌握解析url中的json內容,所以我嘗試了一個小小的練習。我創建了一個小應用程序。我的第一步是從網址獲取內容。當應用程序「停止工作」時,我遇到困難,而且從logcat沒有任何線索顯示我錯在哪裏。從URL中讀取內容時出錯 - 無法從logcat中找到任何線索

活動:

包tri.jsonparser;

//import... 

public class MainActivity extends Activity { 

    URL url; 
     HttpURLConnection conn; 
    String str; 
    Button btn; 
    TextView content; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     btn = (Button) findViewById(R.id.button); 
     content = (TextView) findViewById(R.id.textContent); 

     btn.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       try { 
        url = new URL("http://thinktodo.net/LocationServiceSystem/index.php/location/getLocation?username=1"); 
        conn = (HttpURLConnection) url.openConnection(); 
        Log.d("my tag", "connection ok"); 
        BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
        Log.d("my tag", "reader ok"); 
        str = br.readLine(); 
        content.setText(str); 
        br.close(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 
} 

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <Button 
     android:id="@+id/button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/textViewLat" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="118dp" 
     android:text="Get content" /> 

    <TextView 
     android:id="@+id/textContent" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginTop="58dp" 
     android:text="Content" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

</RelativeLayout> 

的logcat:

04-22 16:24:42.365: E/cutils-trace(1456): Error opening trace file: No such file or directory (2) 
04-22 16:24:42.421: E/memtrack(1456): Couldn't load memtrack module (No such file or directory) 
04-22 16:24:42.421: E/android.os.Debug(1456): failed to load memtrack module: -2 
04-22 16:24:42.737: E/cutils-trace(1467): Error opening trace file: No such file or directory (2) 
04-22 16:24:42.789: E/memtrack(1467): Couldn't load memtrack module (No such file or directory) 
04-22 16:24:42.789: E/android.os.Debug(1467): failed to load memtrack module: -2 
04-22 16:24:43.093: E/OpenGLRenderer(1478): Getting MAX_TEXTURE_SIZE from GradienCache 
04-22 16:24:43.101: E/OpenGLRenderer(1478): MAX_TEXTURE_SIZE: 8192 
04-22 16:24:43.145: E/OpenGLRenderer(1478): Getting MAX_TEXTURE_SIZE from Caches::initConstraints() 
04-22 16:24:43.149: E/OpenGLRenderer(1478): MAX_TEXTURE_SIZE: 8192 
04-22 16:24:45.297: E/AndroidRuntime(1478): FATAL EXCEPTION: main 
04-22 16:24:45.297: E/AndroidRuntime(1478): Process: tri.jsonparser, PID: 1478 
04-22 16:24:45.297: E/AndroidRuntime(1478): android.os.NetworkOnMainThreadException 
04-22 16:24:45.297: E/AndroidRuntime(1478): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145) 
04-22 16:24:45.297: E/AndroidRuntime(1478):  at java.net.InetAddress.lookupHostByName(InetAddress.java:385) 
04-22 16:24:45.297: E/AndroidRuntime(1478): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) 
04-22 16:24:45.297: E/AndroidRuntime(1478): at java.net.InetAddress.getAllByName(InetAddress.java:214) 
04-22 16:24:45.297: E/AndroidRuntime(1478): at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28) 
04-22 16:24:45.297: E/AndroidRuntime(1478): at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216) 
04-22 16:24:45.297: E/AndroidRuntime(1478): at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122) 
04-22 16:24:45.297: E/AndroidRuntime(1478): at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292) 
04-22 16:24:45.297: E/AndroidRuntime(1478): at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255) 
04-22 16:24:45.297: E/AndroidRuntime(1478): at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206) 
04-22 16:24:45.297: E/AndroidRuntime(1478): at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345) 
04-22 16:24:45.297: E/AndroidRuntime(1478): at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296) 
04-22 16:24:45.297: E/AndroidRuntime(1478): at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:179) 
04-22 16:24:45.297: E/AndroidRuntime(1478): at tri.jsonparser.MainActivity$1.onClick(MainActivity.java:41) 
04-22 16:24:45.297: E/AndroidRuntime(1478): at android.view.View.performClick(View.java:4438) 
04-22 16:24:45.297: E/AndroidRuntime(1478): at android.view.View$PerformClick.run(View.java:18422) 
04-22 16:24:45.297: E/AndroidRuntime(1478): at android.os.Handler.handleCallback(Handler.java:733) 
04-22 16:24:45.297: E/AndroidRuntime(1478): at android.os.Handler.dispatchMessage(Handler.java:95) 
04-22 16:24:45.297: E/AndroidRuntime(1478): at android.os.Looper.loop(Looper.java:136) 
04-22 16:24:45.297: E/AndroidRuntime(1478): at android.app.ActivityThread.main(ActivityThread.java:5017) 
04-22 16:24:45.297: E/AndroidRuntime(1478): at java.lang.reflect.Method.invokeNative(Native Method) 
04-22 16:24:45.297: E/AndroidRuntime(1478): at java.lang.reflect.Method.invoke(Method.java:515) 
04-22 16:24:45.297: E/AndroidRuntime(1478): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
04-22 16:24:45.297: E/AndroidRuntime(1478): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
04-22 16:24:45.297: E/AndroidRuntime(1478): at dalvik.system.NativeStart.main(Native Method) 
04-22 16:24:48.121: E/InputDispatcher(547): channel '52a395b4 tri.jsonparser/tri.jsonparser.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed! 
+0

在主UI線程上運行網絡操作。你需要使用一個Thread或者Asynctask。按鈕點擊中的代碼必須在線程中 – Raghunandan

+0

'NetworkOnMainThreadException' +'無法從logcat'認真找到任何線索? – njzk2

+0

@ njzk2我是關於android的新手,我在研究它。我沒有太多的閱讀logcat的經驗。感謝您的幫助。 –

回答

1

你需要實現內部一個Runnable的連接,並在另一個線程上運行它。這是因爲Android不允許您在UI線程上執行網絡請求(這是默認情況下在Activity中使用的)。 Android會執行此操作,因爲您的網絡請求可能需要很長時間才能返回,並且直到此時,UI線程的執行纔會停止(「阻止」)。 嘗試用下面的代碼替換您的btn.setOnClickListener()。

btn.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
      // TODO Auto-generated method stub 
       Runnable r = new Runnable(){ 
       @Override 
       public void run(){ 
      try { 
       url = new URL("http://thinktodo.net/LocationServiceSystem/index.php/location/getLocation?username=1"); 
       conn = (HttpURLConnection) url.openConnection(); 
       Log.d("my tag", "connection ok"); 
       BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
       Log.d("my tag", "reader ok"); 
       str = br.readLine(); 
       Runnable uir = new Runnable(){ 
       @Override 
       public void run() 
       { 
        content.setText(str); 
       } 
       }; 
       MainActivity.this.runOnUiThread(uir); 
       br.close(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
     }; 
     Thread th = new Thread(r); 
     th.start(); 
     } 
    }); 

一旦您的請求與服務器建立連接,您的TextView的內容就會改變。 編輯:正如@ n​​jzk2指出的,設置TextView的內容必須在UI線程上進行。

+0

-1,因爲'content.setText(str);'不會工作。 – njzk2

+0

@ njzk2謝謝你指出。答案已經適當修改。 –

+0

好的。刪除downvote,因爲這是一個工作解決方案,但是,你應該真的考慮asynctasks。 b /這個問題被問及答覆了幾百萬次,因此它應該被重複關閉,而不是回答。 – njzk2