android.os.NetworkOnMainThreadException错误

时间:2014-12-31 15:41:43

标签: java android

我正在尝试创建一个表单,当我们点击它发送到数据库的按钮时 在运行时我的代码一切正常,但当我尝试注册表单时,我有错误消息:error android.os.NetworkOnMainThreadException

你能帮助我!!

    package com.example.pst;

import java.util.ArrayList;

//import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.app.Activity;


import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;   
import org.apache.http.message.BasicNameValuePair;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

    public class MainActivity extends Activity {

        EditText nom,prenom,email,prevente ;
        Button bouton ;
        HttpPost httppost;
        StringBuffer buffer;
        HttpClient httpclient ;

        @Override
     public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.formulaire);

     nom = (EditText)  findViewById(R.id.editTextFormulaire1) ;
     prenom = (EditText) findViewById(R.id.editTextFormulaire2);
     email = (EditText) findViewById(R.id.editTextFormulaire3);
     prevente = (EditText) findViewById(R.id.editTextFormulaire4);
     bouton = (Button) findViewById(R.id.buttonFormulaire1) ;

     String enregistre = bouton.getText().toString();

     bouton.setOnClickListener(new View.OnClickListener()
     {
         public void onClick(View nouveau)

         {
            params[0] = null; 
            new AsyncTask<Void, Void, Void>() {
                @Override

                protected Void doInBackground(Void... params) {


                    httpclient = new DefaultHttpClient();
                    httppost = new HttpPost("http://10.0.2.2:8080");

                      try {
                          ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                          nameValuePairs.add(new BasicNameValuePair("nom", N));
                          nameValuePairs.add(new BasicNameValuePair("prenom", P));
                          nameValuePairs.add(new BasicNameValuePair("email", E));
                          nameValuePairs.add(new BasicNameValuePair("prevente", B));

                          httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));                  
                          HttpResponse response = httpclient.execute(httppost);
                          Log.i("postData", response.getStatusLine().toString());
                           }
                           catch(Exception e)
                           {
                               Log.e("log_tag", "Error:  "+e.toString());
                           }  
                         }
                     };

                    return null;
              }
            }.execute();
           }
          }

我已将此代码放在我的Manifest上:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

1 个答案:

答案 0 :(得分:0)

字面意思是你做错了。

https://developer.android.com/training/basics/network-ops/index.html

  

网络运营可能涉及不可预测的延迟。为了防止这种情况   从导致糟糕的用户体验,始终执行网络操作   在与UI的单独线程上。 AsyncTask类提供了一个   从UI线程启动新任务的最简单方法。更多   讨论这个主题,请参阅博客文章Multithreading For   性能

https://developer.android.com/training/basics/network-ops/connecting


bouton.setOnClickListener(new View.OnClickListener() {
 public void onClick(View nouveau) {
    new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... params) {
            httpclient = new DefaultHttpClient();
            httppost = new HttpPost("http://10.0.2.2:8080");

              try {
                  ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                  nameValuePairs.add(new BasicNameValuePair("nom", N));
                  nameValuePairs.add(new BasicNameValuePair("prenom", P));
                  nameValuePairs.add(new BasicNameValuePair("email", E));
                  nameValuePairs.add(new BasicNameValuePair("prevente", B));

                  httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));                  
                  HttpResponse response = httpclient.execute(httppost);
                  Log.i("postData", response.getStatusLine().toString());
              } catch(Exception e) {
                       Log.e("log_tag", "Error:  "+e.toString());
              }  
            return null;
         }
     }.execute();
   }

}