我有一个项目,我在使用 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)
谢谢!