2013-02-17 103 views
2

以下代碼應訪問讀取XML並在佈局中顯示它的網站。 不知何故是不是在我的Android上工作,而在模擬器上它工作正常。 有人能告訴我爲什麼它不工作,我應該怎麼做才能糾正它?HttpSocket無法在我的Android設備上工作

的Java:

public class MainActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
} 
public void connect (View v){ 
    TextView mTextView = (TextView) findViewById(R.id.textView1); 

     try { 
      Socket mySocket = new Socket("www.ynet.co.il", 80); 
      InputStream in = mySocket.getInputStream(); 
      BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 

      String line = ""; 
      String output = ""; 
      while ((line = reader.readLine()) != null) { 
       output += line; 
      } 
      mTextView.setText(output); 

     } catch (UnknownHostException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

} 

public void good (View v){ 
     TextView mTextView = (TextView) findViewById(R.id.textView1); 
     HttpClient client = new DefaultHttpClient(); 
     HttpGet request = new HttpGet("http://www.boi.org.il/currency.xml?curr=01"); 

     try { 
      HttpResponse response = client.execute(request); 
      HttpEntity entity = response.getEntity(); 
      InputStreamReader in = new InputStreamReader(entity.getContent()); 
      BufferedReader reader = new BufferedReader(in); 

      String line = ""; 
      String output = ""; 
      while ((line = reader.readLine()) != null) { 
       output += line; 

       if (line.contains("RATE")) { 
        StringTokenizer tokens = new StringTokenizer(line,"><"); 
        String token = tokens.nextToken(); 
        String token1 = tokens.nextToken(); 
        String token2 = tokens.nextToken(); 
        mTextView.setText("The USD/ILS Rate is: "+token2); 
       } 
      } 




     } catch (ClientProtocolException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
} 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 

}

這是logcat的:

02-17 17:55:39.165: D/dalvikvm(14064): Late-enabling CheckJNI 
02-17 17:55:39.225: I/dalvikvm(14064): Turning on JNI app bug workarounds for target SDK version 10... 
02-17 17:55:39.280: W/ActivityThread(14064): Application com.example.httpsocket can be debugged on port 8100... 
02-17 17:55:40.905: D/AndroidRuntime(14064): Shutting down VM 
02-17 17:55:40.905: W/dalvikvm(14064): threadid=1: thread exiting with uncaught exception (group=0x410ed2a0) 
02-17 17:55:40.935: E/AndroidRuntime(14064): FATAL EXCEPTION: main 
02-17 17:55:40.935: E/AndroidRuntime(14064): java.lang.IllegalStateException: Could not execute method of the activity 
02-17 17:55:40.935: E/AndroidRuntime(14064): at android.view.View$1.onClick(View.java:3691) 
02-17 17:55:40.935: E/AndroidRuntime(14064): at android.view.View.performClick(View.java:4211) 
02-17 17:55:40.935: E/AndroidRuntime(14064): at android.view.View$PerformClick.run(View.java:17267) 
02-17 17:55:40.935: E/AndroidRuntime(14064): at android.os.Handler.handleCallback(Handler.java:615) 
02-17 17:55:40.935: E/AndroidRuntime(14064): at android.os.Handler.dispatchMessage(Handler.java:92) 
02-17 17:55:40.935: E/AndroidRuntime(14064): at android.os.Looper.loop(Looper.java:137) 
02-17 17:55:40.935: E/AndroidRuntime(14064): at android.app.ActivityThread.main(ActivityThread.java:4898) 
02-17 17:55:40.935: E/AndroidRuntime(14064): at java.lang.reflect.Method.invokeNative(Native Method) 
02-17 17:55:40.935: E/AndroidRuntime(14064): at java.lang.reflect.Method.invoke(Method.java:511) 
02-17 17:55:40.935: E/AndroidRuntime(14064): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006) 
02-17 17:55:40.935: E/AndroidRuntime(14064): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773) 
02-17 17:55:40.935: E/AndroidRuntime(14064): at dalvik.system.NativeStart.main(Native Method) 
02-17 17:55:40.935: E/AndroidRuntime(14064): Caused by: java.lang.reflect.InvocationTargetException 
02-17 17:55:40.935: E/AndroidRuntime(14064): at java.lang.reflect.Method.invokeNative(Native Method) 
02-17 17:55:40.935: E/AndroidRuntime(14064): at java.lang.reflect.Method.invoke(Method.java:511) 
02-17 17:55:40.935: E/AndroidRuntime(14064): at android.view.View$1.onClick(View.java:3686) 
02-17 17:55:40.935: E/AndroidRuntime(14064): ... 11 more 
02-17 17:55:40.935: E/AndroidRuntime(14064): Caused by: android.os.NetworkOnMainThreadException 
02-17 17:55:40.935: E/AndroidRuntime(14064): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118) 
02-17 17:55:40.935: E/AndroidRuntime(14064): at java.net.InetAddress.lookupHostByName(InetAddress.java:385) 
02-17 17:55:40.935: E/AndroidRuntime(14064): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) 
02-17 17:55:40.935: E/AndroidRuntime(14064): at java.net.InetAddress.getAllByName(InetAddress.java:214) 
02-17 17:55:40.935: E/AndroidRuntime(14064): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137) 
02-17 17:55:40.935: E/AndroidRuntime(14064): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) 
02-17 17:55:40.935: E/AndroidRuntime(14064): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119) 
02-17 17:55:40.935: E/AndroidRuntime(14064): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360) 
02-17 17:55:40.935: E/AndroidRuntime(14064): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:670) 
02-17 17:55:40.935: E/AndroidRuntime(14064): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:509) 
02-17 17:55:40.935: E/AndroidRuntime(14064): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) 
02-17 17:55:40.935: E/AndroidRuntime(14064): at com.example.httpsocket.MainActivity.good(MainActivity.java:63) 
02-17 17:55:40.935: E/AndroidRuntime(14064): ... 14 more 

回答

0

NetworkOnMainThreadException當您嘗試訪問網絡,即開放的HttpConnection發生在運行Android與API 11設備和更高版本在主線程上。

當應用程序嘗試在其主線程上執行聯網操作時引發的異常。

這僅適用於針對Honeycomb SDK或 的應用程序。針對早期SDK版本的應用程序允許在其主要事件循環線程上執行聯網,但不鼓勵 。

因此它適用於您的模擬器,因爲它很可能具有小於11的API,並且您的設備具有更新的API。

要解決它使用AsyncTask或網絡連接的專用線程,see this question

相關問題