我正在尝试构建一个 POST 数据包并将其传递到仅包含数据的某个 url:
value={ "y": 2102 }
所以我尝试创建以下类:
class myPostReq {
HttpResponse response = null;
StringEntity tmp = null;
HttpPost httpPost = null;
DefaultHttpClient httpClient;
HttpContext localContext;
public myPostReq() { }
public int post(String url,String data) {
httpClient = new DefaultHttpClient();
httpPost = new HttpPost(url);
localContext = new BasicHttpContext();
int ret = 0;
try {
tmp = new StringEntity("value={ y: " + data + "}","UTF-8");
} catch (UnsupportedEncodingException e) {
ret = -1;
}
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
httpPost.setEntity(tmp);
try {
response = httpClient.execute(httpPost,localContext);
if (response != null) {
ret = response.getStatusLine().getStatusCode();
}
} catch (Exception e) {
ret = -2;
}
return ret;
}
}
但是,当我执行 post(..) 方法时,它一直在崩溃。我不明白我做错了什么。
编辑:提供Logcat:
02-08 18:21:51.841: W/System.err(2034): android.os.NetworkOnMainThreadException
02-08 18:21:51.851: W/System.err(2034): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
02-08 18:21:51.851: W/System.err(2034): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
02-08 18:21:51.851: W/System.err(2034): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
02-08 18:21:51.861: W/System.err(2034): at java.net.InetAddress.getAllByName(InetAddress.java:214)
02-08 18:21:51.861: W/System.err(2034): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
02-08 18:21:51.871: W/System.err(2034): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
02-08 18:21:51.871: W/System.err(2034): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
02-08 18:21:51.871: W/System.err(2034): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
02-08 18:21:51.881: W/System.err(2034): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
02-08 18:21:51.881: W/System.err(2034): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
02-08 18:21:51.881: W/System.err(2034): at com.example.remoteswitch.myPostReq.post(MainActivity.java:266)
02-08 18:21:51.891: W/System.err(2034): at com.example.remoteswitch.MainActivity.startOperation(MainActivity.java:198)
02-08 18:21:51.891: W/System.err(2034): at java.lang.reflect.Method.invokeNative(Native Method)
02-08 18:21:51.891: W/System.err(2034): at java.lang.reflect.Method.invoke(Method.java:511)
02-08 18:21:51.891: W/System.err(2034): at android.view.View$1.onClick(View.java:3592)
02-08 18:21:51.901: W/System.err(2034): at android.view.View.performClick(View.java:4202)
02-08 18:21:51.901: W/System.err(2034): at android.view.View$PerformClick.run(View.java:17340)
02-08 18:21:51.901: W/System.err(2034): at android.os.Handler.handleCallback(Handler.java:725)
02-08 18:21:51.911: W/System.err(2034): at android.os.Handler.dispatchMessage(Handler.java:92)
02-08 18:21:51.911: W/System.err(2034): at android.os.Looper.loop(Looper.java:137)
02-08 18:21:51.911: W/System.err(2034): at android.app.ActivityThread.main(ActivityThread.java:5039)
02-08 18:21:51.921: W/System.err(2034): at java.lang.reflect.Method.invokeNative(Native Method)
02-08 18:21:51.921: W/System.err(2034): at java.lang.reflect.Method.invoke(Method.java:511)
02-08 18:21:51.941: W/System.err(2034): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-08 18:21:51.941: W/System.err(2034): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-08 18:21:51.941: W/System.err(2034): at dalvik.system.NativeStart.main(Native Method)
02-08 18:21:52.080: I/Choreographer(2034): Skipped 66 frames! The application may be doing too much work on its main thread.
02-08 18:21:52.080: W/Trace(2034): Unexpected value from nativeGetEnabledTags: 0