2

以下代码应访问网站并读取 XML 并将其显示在布局上。在模拟器上运行正常时,不知何故无法在我的 Android 上运行。有人可以告诉我为什么它不起作用,我应该怎么做才能纠正它?

爪哇:

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
4

1 回答 1

0

当您尝试访问网络(即在主线程上打开 HttpConnection)NetworkOnMainThreadException时,在运行 android 的设备上会发生很好的情况。API 11

当应用程序尝试在其主线程上执行网络操作时引发的异常。

这仅针对面向 Honeycomb SDK 或更高版本的应用程序抛出。允许以早期 SDK 版本为目标的应用程序在其主事件循环线程上进行网络连接,但非常不鼓励这样做。

所以它可以在你的模拟器上运行,因为很可能它的 API 少于 11,并且你的设备有更新的 API。

要解决它,请使用AsyncTask网络连接或专用线程,请参阅此问题

于 2013-02-17T16:10:38.350 回答