将字符串从Android发布到PHP

时间:2014-08-15 02:04:20

标签: php android mysql

首先,请提前感谢您提供的任何帮助。我目前正在尝试将我的Android应用程序连接到mysql数据库(Xampp)。以下是我在Android中使用的代码

public void submitRecipe(View view) throws IOException
    {
    URL u = new URL("http://localhost/phpconnect.php");
    String data  = "recipe="+"mar";
    HttpURLConnection conn = (HttpURLConnection) u.openConnection(); 
    conn.setDoOutput(true);
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    conn.setRequestMethod("POST");
    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); 
    wr.write(data);
    wr.flush();
    wr.close();

在wr.write(数据)之后,应用程序总是崩溃。我知道我没有在这个小代码段中发布任何有意义的信息,但我只是想让它立即发挥作用。 Php代码如下。提前感谢您提供任何帮助,因为我是Android的新手,我只是想让我的数据库连接起作用。

    <?php
    if(empty($_POST['recipe'])
{
    echo "BAD";
}
else
{
    $alpha = "recipe";

    // Try to connect to the database.
    $database=mysqli_connect("localhost","Michael","12345");
    mysqli_select_db($database,"customers");

    // If failed
    if (mysqli_connect_errno()) {
      // The database cannot be connected to.
      echo "Database could not be connected to";
      return;
    }

    $query = mysqli_query($database,"Select * From recipes Where Url LIKE '%" + $alpha + "%'");

    if($query==FALSE)
    {

    }
    else
    {
        while($row=mysql_fetch_assoc($query))
        {
            $output[]=$row;
        }
        echo "GOOD";
        mysqli_close($database);
    }
}

?>

08-14 22:10:44.553:E / AndroidRuntime(5001):致命异常:主要 08-14 22:10:44.553:E / AndroidRuntime(5001):进程:com.example.websiteapp,PID:5001 08-14 22:10:44.553:E / AndroidRuntime(5001):java.lang.IllegalStateException:无法执行活动的方法 08-14 22:10:44.553:E / AndroidRuntime(5001):在android.view.View $ 1.onClick(View.java:3823) 08-14 22:10:44.553:E / AndroidRuntime(5001):在android.view.View.performClick(View.java:4438) 08-14 22:10:44.553:E / AndroidRuntime(5001):在android.view.View $ PerformClick.run(View.java:18422) 08-14 22:10:44.553:E / AndroidRuntime(5001):在android.os.Handler.handleCallback(Handler.java:733) 08-14 22:10:44.553:E / AndroidRuntime(5001):在android.os.Handler.dispatchMessage(Handler.java:95) 08-14 22:10:44.553:E / AndroidRuntime(5001):在android.os.Looper.loop(Looper.java:136) 08-14 22:10:44.553:E / AndroidRuntime(5001):在android.app.ActivityThread.main(ActivityThread.java:5017) 08-14 22:10:44.553:E / AndroidRuntime(5001):at java.lang.reflect.Method.invokeNative(Native Method) 08-14 22:10:44.553:E / AndroidRuntime(5001):at java.lang.reflect.Method.invoke(Method.java:515) 08-14 22:10:44.553:E / AndroidRuntime(5001):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779) 08-14 22:10:44.553:E / AndroidRuntime(5001):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 08-14 22:10:44.553:E / AndroidRuntime(5001):at dalvik.system.NativeStart.main(Native Method) 08-14 22:10:44.553:E / AndroidRuntime(5001):引起:java.lang.reflect.InvocationTargetException 08-14 22:10:44.553:E / AndroidRuntime(5001):at java.lang.reflect.Method.invokeNative(Native Method) 08-14 22:10:44.553:E / AndroidRuntime(5001):at java.lang.reflect.Method.invoke(Method.java:515) 08-14 22:10:44.553:E / AndroidRuntime(5001):在android.view.View $ 1.onClick(View.java:3818) 08-14 22:10:44.553:E / AndroidRuntime(5001):... 11更多 08-14 22:10:44.553:E / AndroidRuntime(5001):引起:android.os.NetworkOnMainThreadException 08-14 22:10:44.553:E / AndroidRuntime(5001):在android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145) 08-14 22:10:44.553:E / AndroidRuntime(5001):at java.net.InetAddress.lookupHostByName(InetAddress.java:385) 08-14 22:10:44.553:E / AndroidRuntime(5001):at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) 08-14 22:10:44.553:E / AndroidRuntime(5001):at java.net.InetAddress.getAllByName(InetAddress.java:214) 08-14 22:10:44.553:E / AndroidRuntime(5001):at com.android.okhttp.internal.Dns $ 1.getAllByName(Dns.java:28) 08-14 22:10:44.553:E / AndroidRuntime(5001):at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216) 08-14 22:10:44.553:E / AndroidRuntime(5001):at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122) 08-14 22:10:44.553:E / AndroidRuntime(5001):at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292) 08-14 22:10:44.553:E / AndroidRuntime(5001):at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255) 08-14 22:10:44.553:E / AndroidRuntime(5001):at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206) 08-14 22:10:44.553:E / AndroidRuntime(5001):at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345) 08-14 22:10:44.553:E / AndroidRuntime(5001):at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89) 08-14 22:10:44.553:E / AndroidRuntime(5001):at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:197) 08-14 22:10:44.553:E / AndroidRuntime(5001):at com.example.websiteapp.RecipePage.submitRecipe(RecipePage.java:94) 08-14 22:10:44.553:E / AndroidRuntime(5001):... 14更多

1 个答案:

答案 0 :(得分:1)

您的崩溃与服务器端代码无关 - 您尝试在主(UI)线程上执行网络调用,即NetworkOnMainThreadException。尝试在AsyncTask的doInBackground中运行它。