-2

HttpClientを使用しているプロジェクトがありますが、クラッシュします。

私のjsonは正常にログを記録しています。

私はインターネットの許可を得ています、

HTTPClientでリクエストをテストし、機能しています。

しかし、なぜクラッシュするのかわかりませんか?

ここに私のコード、

public void empaquetaYmanda() {

        Log.d("mensa", "empaquetaYmanda copio");


        //build my dictionary!
        Map<String, String> comment = new HashMap<String, String>();
        comment.put("resource_uri", "pa que esto");
        comment.put("guid", "345");
        comment.put("dob", "13-07-1990");
        comment.put("address", "23 rue sante");
        comment.put("name", "Sakuraba");
        comment.put("mobile", "333456888");
        comment.put("email", "saku@sa.sss");
        comment.put("points", "333999");
        comment.put("surname", "Cerezin");
        comment.put("devicetoken", "token334455Droid");

        Log.d("mensa", "tu dicto :: " + comment);

        //lo serializo dicto a json
        String json = new GsonBuilder().create().toJson(comment, Map.class);

        Log.d("mensa", "tu json :: " + json);

        postData("http://sasa.com/api/v1/client/", json); //my

    }


    public void postData(String uri, String json) {
        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();

        HttpPost httppost = new HttpPost(uri);


     // Url Encoding the POST parameters
            try {
                Log.d("mensa", "tu Encoding");

                httppost.setEntity(new StringEntity(json)); //revisa!
                //httppost.setHeader("Accept", "application/json");
                httppost.setHeader("Content-type", "application/json");

            } catch (UnsupportedEncodingException e) {
                // writing error to Log
                e.printStackTrace();
            }

            Log.d("mensa", "tu request::"+httppost);


            // Making HTTP Request
            try {
                HttpResponse response = httpclient.execute(httppost);

                // writing response to log
                Log.d("Http Response:", response.toString());
                Log.d("mensa", "tu respuesta ::"+response.toString());

            } catch (ClientProtocolException e) {
                // writing exception to log
                e.printStackTrace();
            } catch (IOException e) {
                // writing exception to log
                e.printStackTrace();

            }

    }

ここにエラーログ

02-22 06:17:40.980: E/AndroidRuntime(19016): FATAL EXCEPTION: main
02-22 06:17:40.980: E/AndroidRuntime(19016): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hyper.gsonex/com.hyper.gsonex.Main}: android.os.NetworkOnMainThreadException
02-22 06:17:40.980: E/AndroidRuntime(19016):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
02-22 06:17:40.980: E/AndroidRuntime(19016):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-22 06:17:40.980: E/AndroidRuntime(19016):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-22 06:17:40.980: E/AndroidRuntime(19016):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-22 06:17:40.980: E/AndroidRuntime(19016):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-22 06:17:40.980: E/AndroidRuntime(19016):    at android.os.Looper.loop(Looper.java:137)
02-22 06:17:40.980: E/AndroidRuntime(19016):    at android.app.ActivityThread.main(ActivityThread.java:5039)
02-22 06:17:40.980: E/AndroidRuntime(19016):    at java.lang.reflect.Method.invokeNative(Native Method)
02-22 06:17:40.980: E/AndroidRuntime(19016):    at java.lang.reflect.Method.invoke(Method.java:511)
02-22 06:17:40.980: E/AndroidRuntime(19016):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-22 06:17:40.980: E/AndroidRuntime(19016):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-22 06:17:40.980: E/AndroidRuntime(19016):    at dalvik.system.NativeStart.main(Native Method)
02-22 06:17:40.980: E/AndroidRuntime(19016): Caused by: android.os.NetworkOnMainThreadException
02-22 06:17:40.980: E/AndroidRuntime(19016):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
02-22 06:17:40.980: E/AndroidRuntime(19016):    at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
02-22 06:17:40.980: E/AndroidRuntime(19016):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
02-22 06:17:40.980: E/AndroidRuntime(19016):    at java.net.InetAddress.getAllByName(InetAddress.java:214)
02-22 06:17:40.980: E/AndroidRuntime(19016):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
02-22 06:17:40.980: E/AndroidRuntime(19016):    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
02-22 06:17:40.980: E/AndroidRuntime(19016):    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
02-22 06:17:40.980: E/AndroidRuntime(19016):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
02-22 06:17:40.980: E/AndroidRuntime(19016):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
02-22 06:17:40.980: E/AndroidRuntime(19016):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
02-22 06:17:40.980: E/AndroidRuntime(19016):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
02-22 06:17:40.980: E/AndroidRuntime(19016):    at com.hyper.gsonex.Main.postData(Main.java:116)
02-22 06:17:40.980: E/AndroidRuntime(19016):    at com.hyper.gsonex.Main.empaquetaYmanda(Main.java:85)
02-22 06:17:40.980: E/AndroidRuntime(19016):    at com.hyper.gsonex.Main.onCreate(Main.java:46)
02-22 06:17:40.980: E/AndroidRuntime(19016):    at android.app.Activity.performCreate(Activity.java:5104)
02-22 06:17:40.980: E/AndroidRuntime(19016):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-22 06:17:40.980: E/AndroidRuntime(19016):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)

ありがとう!

4

2 に答える 2

1

メインスレッドでHTTPリクエストを行うことはできません。AsyncTaskまたはスレッドで行う必要があります

于 2013-02-21T19:27:18.600 に答える
1

UIスレッドでネットワーク操作を実行しようとしているため、NetWorkOnUIThreadExceptionがスローされます。ネットワーク関連のコードを別のスレッドに保持して実行してみてください。より良い解決策は、非同期タスクを使用することです。

于 2013-02-21T19:32:31.477 に答える