-2

I'm following the first answer on this question, and it doesn't work! I don't know why; please, if someone can help me, it would be appreciated.

This is my code:

package com.example.enviarphp;

//All the import is here

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

     // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://appandroid.comuv.com/index.php");

    try {
        // Add your data
        Log.i( "Anda el try", "anda el try");
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
        nameValuePairs.add(new BasicNameValuePair("dato", "Probandooo"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        Log.i( "se esta por ejecutar", "se esta por ejecutar");
        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);
        Log.i( "se ejecutar", "se ejecutar");
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        throw new AssertionError("IO Error");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        throw new AssertionError("Client Protocol Error");

    }
}

}

And this is my PHP file:

<?php

$conexion = mysql_connect("base","name","password") or die ("Error al conectar a la base de datos.");

 mysql_select_db("name2",$conexion);

 $dato = $_POST['dato'];

 $sql="INSERT INTO Mensajes(id,mensaje) VALUES(null,$dato)";

 mysql_query($sql);

 ?>

LogCat error:

06-17 16:09:24.779: I/Anda el try(9916): anda el try
06-17 16:09:24.829: I/se esta por ejecutar(9916): se esta por ejecutar
06-17 16:09:24.919: D/AndroidRuntime(9916): Shutting down VM
06-17 16:09:24.919: W/dalvikvm(9916): threadid=1: thread exiting with uncaught exception (group=0x40a4b1f8)
06-17 16:09:24.999: E/AndroidRuntime(9916): FATAL EXCEPTION: main
06-17 16:09:24.999: E/AndroidRuntime(9916): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.enviarphp/com.example.enviarphp.MainActivity}: android.os.NetworkOnMainThreadException
06-17 16:09:24.999: E/AndroidRuntime(9916):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2079)
06-17 16:09:24.999: E/AndroidRuntime(9916):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
06-17 16:09:24.999: E/AndroidRuntime(9916):     at android.app.ActivityThread.access$600(ActivityThread.java:132)
06-17 16:09:24.999: E/AndroidRuntime(9916):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1157)
06-17 16:09:24.999: E/AndroidRuntime(9916):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-17 16:09:24.999: E/AndroidRuntime(9916):     at android.os.Looper.loop(Looper.java:137)
06-17 16:09:24.999: E/AndroidRuntime(9916):     at android.app.ActivityThread.main(ActivityThread.java:4575)
06-17 16:09:24.999: E/AndroidRuntime(9916):     at java.lang.reflect.Method.invokeNative(Native Method)
06-17 16:09:24.999: E/AndroidRuntime(9916):     at java.lang.reflect.Method.invoke(Method.java:511)
06-17 16:09:24.999: E/AndroidRuntime(9916):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
06-17 16:09:24.999: E/AndroidRuntime(9916):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
06-17 16:09:24.999: E/AndroidRuntime(9916):     at dalvik.system.NativeStart.main(Native Method)
06-17 16:09:24.999: E/AndroidRuntime(9916): Caused by: android.os.NetworkOnMainThreadException
06-17 16:09:24.999: E/AndroidRuntime(9916):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
06-17 16:09:24.999: E/AndroidRuntime(9916):     at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
06-17 16:09:24.999: E/AndroidRuntime(9916):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
06-17 16:09:24.999: E/AndroidRuntime(9916):     at java.net.InetAddress.getAllByName(InetAddress.java:220)
06-17 16:09:24.999: E/AndroidRuntime(9916):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
06-17 16:09:24.999: E/AndroidRuntime(9916):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
06-17 16:09:24.999: E/AndroidRuntime(9916):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
06-17 16:09:24.999: E/AndroidRuntime(9916):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
06-17 16:09:24.999: E/AndroidRuntime(9916):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
06-17 16:09:24.999: E/AndroidRuntime(9916):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
06-17 16:09:24.999: E/AndroidRuntime(9916):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
06-17 16:09:24.999: E/AndroidRuntime(9916):     at com.example.enviarphp.MainActivity.onCreate(MainActivity.java:39)
06-17 16:09:24.999: E/AndroidRuntime(9916):     at android.app.Activity.performCreate(Activity.java:4465)
06-17 16:09:24.999: E/AndroidRuntime(9916):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-17 16:09:24.999: E/AndroidRuntime(9916):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2033)
06-17 16:09:24.999: E/AndroidRuntime(9916):     ... 11 more

Thanks a lot! I hope somebody can help me with my problem, I've tried everything I know.

Community
  • 1
  • 1
user2212773
  • 128
  • 1
  • 3
  • 9

1 Answers1

1

Caused by: android.os.NetworkOnMainThreadException

You are performing network I/O on the main application thread. You need to move this to a background thread, perhaps one managed by an AsyncTask.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491