我正在构建一个通过 HttpPost 将变量传递给我的服务器的应用程序。在我昨天将我的 Android 2.3 设备更新到 Android 4.0 之前,一切都运行良好。更新后,我正在处理的应用程序在每次运行 HttpPost 时都会崩溃。我认为由于相同的代码在 Android 2.3 中运行,问题在于它与更新的系统不兼容。
我可以传递相同的变量并仍然与 Android 2.3 - 4.0 兼容的最佳方式是什么?这里的最佳做法是什么?我对移动开发相当陌生。我的方法论如何?
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("MY URL");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("receiver", "BLAH"));
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try {
httpClient.execute(httpPost);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
更新
这是我的 LogCat
07-06 10:41:25.385: D/DroidGap(6856): DroidGap.onCreate()
07-06 10:42:09.205: I/dalvikvm(7330): Turning on JNI app bug workarounds for target SDK version 10...
07-06 10:42:09.275: D/AndroidRuntime(7330): Shutting down VM
07-06 10:42:09.275: W/dalvikvm(7330): threadid=1: thread exiting with uncaught exception (group=0x40acd210)
07-06 10:42:09.275: E/AndroidRuntime(7330): FATAL EXCEPTION: main
07-06 10:42:09.275: E/AndroidRuntime(7330): java.lang.RuntimeException: Unable to start receiver tellatext.sms.app.SmsReceiver: android.os.NetworkOnMainThreadException
07-06 10:42:09.275: E/AndroidRuntime(7330): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2229)
07-06 10:42:09.275: E/AndroidRuntime(7330): at android.app.ActivityThread.access$1500(ActivityThread.java:134)
07-06 10:42:09.275: E/AndroidRuntime(7330): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
07-06 10:42:09.275: E/AndroidRuntime(7330): at android.os.Handler.dispatchMessage(Handler.java:99)
07-06 10:42:09.275: E/AndroidRuntime(7330): at android.os.Looper.loop(Looper.java:137)
07-06 10:42:09.275: E/AndroidRuntime(7330): at android.app.ActivityThread.main(ActivityThread.java:4697)
07-06 10:42:09.275: E/AndroidRuntime(7330): at java.lang.reflect.Method.invokeNative(Native Method)
07-06 10:42:09.275: E/AndroidRuntime(7330): at java.lang.reflect.Method.invoke(Method.java:511)
07-06 10:42:09.275: E/AndroidRuntime(7330): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
07-06 10:42:09.275: E/AndroidRuntime(7330): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
07-06 10:42:09.275: E/AndroidRuntime(7330): at dalvik.system.NativeStart.main(Native Method)
07-06 10:42:09.275: E/AndroidRuntime(7330): Caused by: android.os.NetworkOnMainThreadException
07-06 10:42:09.275: E/AndroidRuntime(7330): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1119)
07-06 10:42:09.275: E/AndroidRuntime(7330): at java.net.InetAddress.lookupHostByName(InetAddress.java:441)
07-06 10:42:09.275: E/AndroidRuntime(7330): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:243)
07-06 10:42:09.275: E/AndroidRuntime(7330): at java.net.InetAddress.getAllByName(InetAddress.java:220)
07-06 10:42:09.275: E/AndroidRuntime(7330): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
07-06 10:42:09.275: E/AndroidRuntime(7330): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
07-06 10:42:09.275: E/AndroidRuntime(7330): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
07-06 10:42:09.275: E/AndroidRuntime(7330): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
07-06 10:42:09.275: E/AndroidRuntime(7330): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
07-06 10:42:09.275: E/AndroidRuntime(7330): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
07-06 10:42:09.275: E/AndroidRuntime(7330): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
07-06 10:42:09.275: E/AndroidRuntime(7330): at tellatext.sms.app.SmsReceiver.onReceive(SmsReceiver.java:114)
07-06 10:42:09.275: E/AndroidRuntime(7330): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2221)
07-06 10:42:09.275: E/AndroidRuntime(7330): ... 10 more