1

我的应用程序适用于 android 2.3.3 但它在 android 4.1.2 中强制关闭 下面是我将数据从 android 设备发送到服务器的代码。

HttpEntity entity;
HttpClient client = new DefaultHttpClient();
String url ="http://67.23.166.35:80/android/insertv2.php";

HttpPost request = new HttpPost(url);
StringEntity se = new StringEntity(datatoServer);
se.setContentEncoding("UTF-8");
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));
entity = se;
request.setEntity(entity);
HttpResponse response = client.execute(request);
entity = response.getEntity();
4

3 回答 3

1

Check following links buddy,

I am assuming that you are getting NetworkOnMainThreadException so you should use AsyncTask and RunOnUiThread methods for sending data to server

for Implement AsyncTask

Link1 Link2 Link3 Link4

Hope it will help you.

于 2013-11-14T06:46:16.813 回答
0

不要在主线程中进行网络操作。使用 AyncTask

例子:

 AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {

    @Override
    protected void onPreExecute() {
         //Show UI

    }

    @Override
    protected Void doInBackground(Void... arg0) {
         // do your background process 
        HttpEntity entity;
        HttpClient client = new DefaultHttpClient();
        String url ="http://67.23.166.35:80/android/insertv2.php";

        HttpPost request = new HttpPost(url);
        StringEntity se = new StringEntity(datatoServer);
        se.setContentEncoding("UTF-8");
        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));
        entity = se;
        request.setEntity(entity);
        HttpResponse response = client.execute(request);
        entity = response.getEntity();
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
                      //Show UI (Toast msg here)

    }

    };

    task.execute((Void[])null);

否则你可以添加

 super.onCreate(savedInstanceState);

         StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();
        StrictMode.setThreadPolicy(policy);
于 2013-11-14T07:35:28.687 回答
0

Can you paste the exception you are getting from ddms-log when force close happens. That way we can get the exact idea why this is happening. From what i am guessing you are probably doing network operation in Main Thread(UI) which is not allowed after Android 3.0. Please check out this post for details. I might be wrong about that since i am assuming.

AsyncTask:

Thread :

new Thread(){
  public void run(){
    HttpEntity entity;
    HttpClient client = new DefaultHttpClient();
    String url ="http://67.23.166.35:80/android/insertv2.php";

    HttpPost request = new HttpPost(url);
    StringEntity se = new StringEntity(datatoServer);
    se.setContentEncoding("UTF-8");
    se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));
    entity = se;
    request.setEntity(entity);
    HttpResponse response = client.execute(request);
    entity = response.getEntity();
        runOnUiThread(new Runnable() {

            public void run() {
                // TODO Auto-generated method stub
                // do what you have to do
            }
    });
  }
}.start();
于 2013-11-14T06:44:49.593 回答