0

This is the code:

public boolean add(){

    HttpClient cliente;
    List<NameValuePair> lista;
    HttpPost post;
    cliente = new DefaultHttpClient();
    post = new HttpPost("http://pruebamysqlandroid.esy.es/conexion/conexion.php");
    lista = new ArrayList<NameValuePair>(3);
    lista.add(new BasicNameValuePair("name", contactname.getText().toString()));
    lista.add(new BasicNameValuePair("numbretlf", tlf.getText().toString()));
    lista.add(new BasicNameValuePair("email", email.getText().toString()));
    try{
        post.setEntity(new UrlEncodedFormEntity(lista));
        cliente.execute(post);//here is the error
        return true;
    }catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return false;
}

When i try to add the contact to the server an error dont let me did help me please

The error is :

android.os.NetworkOnMainThreadException

soya666
  • 361
  • 1
  • 4
  • 18
  • Possible duplicate of [How to fix android.os.NetworkOnMainThreadException?](http://stackoverflow.com/questions/6343166/how-to-fix-android-os-networkonmainthreadexception) – Allan Pereira May 15 '16 at 15:16
  • Try this volley libraray it maintains the queues of you request and handles other matters very effeiciantly http://developer.android.com/training/volley/index.html – Adeel Turk May 15 '16 at 15:24
  • Try make this in AsynTask. That what you tells the error. And this is [duplicate question](https://stackoverflow.com/a/6343299/2685996) – xAqweRx May 15 '16 at 15:20

3 Answers3

0

You connect to network in UI thread (Main thread). For example use AsyncTask to connect in other thread.

soya666
  • 361
  • 1
  • 4
  • 18
0

You can not do any network in Main thread,this will cause ANR. See AsyncTask or Volley

zys
  • 1,306
  • 3
  • 18
  • 37
0

You need to move your code in a separate Thread. e.g. Thread, HandlerThread or AsyncTask.

Enzokie
  • 7,365
  • 6
  • 33
  • 39