質問

We want to run a php script on our server to insert data (in the variable nameValuePairs) into our database using HttpClient and HttpPost. If we create the following code as an Android Application with Idea IntelliJ, it works just fine. But if it's created in Eclipse the App crashes at the .execute() method and throws a normal Exception "null".

HttpClient httpclient = new DefaultHttpClient(httpParameters);
HttpPost httppost = new HttpPost("http://bbpf.bplaced.net/insert.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);

Are you familiar with that problem or why does the same program created in Eclipse crash and run in IntelliJ?

EDIT: Here's the Error Log in Eclipse:

04-04 08:11:33.429: E/AndroidRuntime(2888): FATAL EXCEPTION: main
04-04 08:11:33.429: E/AndroidRuntime(2888): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mobile.inserttest/com.mobile.inserttest.MainActivity}: android.os.NetworkOnMainThreadException
04-04 08:11:33.429: E/AndroidRuntime(2888):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
04-04 08:11:33.429: E/AndroidRuntime(2888):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
04-04 08:11:33.429: E/AndroidRuntime(2888):     at android.app.ActivityThread.access$600(ActivityThread.java:127)
04-04 08:11:33.429: E/AndroidRuntime(2888):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
04-04 08:11:33.429: E/AndroidRuntime(2888):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-04 08:11:33.429: E/AndroidRuntime(2888):     at android.os.Looper.loop(Looper.java:137)
04-04 08:11:33.429: E/AndroidRuntime(2888):     at android.app.ActivityThread.main(ActivityThread.java:4441)
04-04 08:11:33.429: E/AndroidRuntime(2888):     at java.lang.reflect.Method.invokeNative(Native Method)
04-04 08:11:33.429: E/AndroidRuntime(2888):     at java.lang.reflect.Method.invoke(Method.java:511)
04-04 08:11:33.429: E/AndroidRuntime(2888):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-04 08:11:33.429: E/AndroidRuntime(2888):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-04 08:11:33.429: E/AndroidRuntime(2888):     at dalvik.system.NativeStart.main(Native Method)
04-04 08:11:33.429: E/AndroidRuntime(2888): Caused by: android.os.NetworkOnMainThreadException
04-04 08:11:33.429: E/AndroidRuntime(2888):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1108)
04-04 08:11:33.429: E/AndroidRuntime(2888):     at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
04-04 08:11:33.429: E/AndroidRuntime(2888):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
04-04 08:11:33.429: E/AndroidRuntime(2888):     at java.net.InetAddress.getAllByName(InetAddress.java:220)
04-04 08:11:33.429: E/AndroidRuntime(2888):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:143)
04-04 08:11:33.429: E/AndroidRuntime(2888):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
04-04 08:11:33.429: E/AndroidRuntime(2888):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
04-04 08:11:33.429: E/AndroidRuntime(2888):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
04-04 08:11:33.429: E/AndroidRuntime(2888):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:580)
04-04 08:11:33.429: E/AndroidRuntime(2888):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:512)
04-04 08:11:33.429: E/AndroidRuntime(2888):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:490)
04-04 08:11:33.429: E/AndroidRuntime(2888):     at com.mobile.inserttest.DBConnection.insert(DBConnection.java:170)
04-04 08:11:33.429: E/AndroidRuntime(2888):     at com.mobile.inserttest.MainActivity.onCreate(MainActivity.java:29)
04-04 08:11:33.429: E/AndroidRuntime(2888):     at android.app.Activity.performCreate(Activity.java:4465)
04-04 08:11:33.429: E/AndroidRuntime(2888):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-04 08:11:33.429: E/AndroidRuntime(2888):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
04-04 08:11:33.429: E/AndroidRuntime(2888):     ... 11 more

Thanks for all help!

役に立ちましたか?

解決

You should make your HTTP request in a separate thread. NetworkOnMainThreadException says that you are trying to make your network operation on the main thread.

docs at developer.android.com

1] Use simple Thread

    Thread t = new Thread(new Runnable() {
        @Override
        public void run() {

            HttpClient httpclient = new DefaultHttpClient(httpParameters);
            HttpPost httppost = new HttpPost("http://bbpf.bplaced.net/insert.php");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            httpclient.execute(httppost);

        }
    });
    t.start();

2] You can use AsyncTask if you would like to have more control over the task:

    AsyncTask<Void, Void, Void> asyncTask = new AsyncTask<Void, Void, Void>() {

        @Override
        protected Void doInBackground(Void... params) {
            ttpClient httpclient = new DefaultHttpClient(httpParameters);
            HttpPost httppost = new HttpPost("http://bbpf.bplaced.net/insert.php");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            httpclient.execute(httppost);

            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            // Notifies UI when the task is done
            textView.setText("Insert finished!");
        }
    }.execute();

3] Use some networking library (robospice, retrofit) to make writing async network tasks a little easier.

他のヒント

android.os.NetworkOnMainThreadException throws because you are performing network operation on main thread .thats why its get crashed.

move your network call code in Asyntask doInBackGround method and then try

private class LongOperation extends AsyncTask<Void, Void, Void> {


    @Override
    protected void onPreExecute() {
    }

    @Override
    protected String doInBackground(String... params) {
         HttpClient httpclient = new DefaultHttpClient(httpParameters);
         HttpPost httppost = new HttpPost("http://bbpf.bplaced.net/insert.php");
         httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
         httpclient.execute(httppost);
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
    }

}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top