Eclipse中的HttpClient execute()崩溃

时间:2014-04-03 17:37:31

标签: android eclipse intellij-idea httpclient

我们希望在我们的服务器上运行php脚本,使用HttpClient和HttpPost将数据(在变量nameValuePairs中)插入到我们的数据库中。 如果我们使用Idea IntelliJ创建以下代码作为Android应用程序,它可以正常工作。 但是如果它是在Eclipse中创建的,则应用程序会在.execute()方法中崩溃,并抛出一个正常的异常“null”。

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

您是否熟悉该问题或为什么在Eclipse中创建的相同程序崩溃并在IntelliJ中运行?

编辑:这是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

感谢您的帮助!

2 个答案:

答案 0 :(得分:5)

您应该在单独的线程中发出HTTP请求。 NetworkOnMainThreadException表示您正在尝试在主线程上进行网络操作。

docs at developer.android.com

1]使用简单的线程

    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]如果您希望对任务有更多控制权,可以使用AsyncTask:

    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]使用一些网络库(robospiceretrofit)可以更轻松地编写异步网络任务。

答案 1 :(得分:1)

android.os.NetworkOnMainThreadException会因为你在main thread上执行网络操作而抛出。这就是为什么它会崩溃。

使用Asyntask doInBackGround方法移动您的网络电话代码,然后尝试

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) {
    }

}