0

我正在构建一个通过 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
4

4 回答 4

1

Android 3.0 及更高版本不允许在主 UI 线程上发送网络请求。

于 2012-07-06T15:43:59.217 回答
0

您需要在后台进程中运行您的 URL 请求。

这是示例代码

new TestTask().execute();

public class TestTask extends AsyncTask<Void, Void, Void>{

    @Override
    protected Void doInBackground(Void... params) {

        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();
           }
        return null;
    }

    }
于 2012-07-06T15:40:57.797 回答
0

好像您正在主线程上运行网络任务。

在 AsyncTask 中运行您的网络任务,因此它不会阻塞您的 UI 线程。永远不要在你的 UI 线程中运行阻塞任务。这是一个教程:http: //developer.android.com/reference/android/os/AsyncTask.html

于 2012-07-06T15:44:10.000 回答
0
  1. 确保您使用的是 Asynctask(正如其他人所提到的)
  2. 你在使用代理服务器吗?通常如果您通过 3G 连接到 Internet,移动运营商会在您的设备上自动设置代理服务器 + 端口。如果您正在使用它,请尝试清除它并再次运行您的应用程序。
于 2012-07-06T15:50:41.253 回答