-3

I'm trying to create a form in which when we click on the button it send to the data base When in run my code everything is ok but when i try to register the form I have that error message : error android.os.NetworkOnMainThreadException

Can you help me!!

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

I already put this code on my Manifest:

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

1 Answers1

0

Literally you're doing it wrong.

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

Network operations can involve unpredictable delays. To prevent this from causing a poor user experience, always perform network operations on a separate thread from the UI. The AsyncTask class provides one of the simplest ways to fire off a new task from the UI thread. For more discussion of this topic, see the blog post Multithreading For Performance.

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

}

Blundell
  • 75,855
  • 30
  • 208
  • 233