-2

我正在关注这个问题的第一个答案,但它不起作用!我不知道为什么;请,如果有人可以帮助我,将不胜感激。

这是我的代码:

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");

    }
}

}

这是我的 PHP 文件:

<?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 错误:

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

非常感谢!我希望有人可以帮助我解决我的问题,我已经尝试了我所知道的一切。

4

1 回答 1

1

引起:android.os.NetworkOnMainThreadException

您正在主应用程序线程上执行网络 I/O。您需要将其移至后台线程,可能是由AsyncTask.

于 2013-06-17T19:39:26.063 回答