0

First of all, thank you in advance for any help you can give. I am currently trying to connect my android application to a mysql database (Xampp). Below is the code that I am using in 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();

After wr.write(data), the application always crashes. I know that I am not posting any meaningful information with this small code segment, but I am just trying to get it to work right now. Php code is below. Thank you in advance for any help you can give as I am new to Android and am just trying to get my connection to my database to work.

    <?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): FATAL EXCEPTION: main 08-14 22:10:44.553: E/AndroidRuntime(5001): Process: com.example.websiteapp, PID: 5001 08-14 22:10:44.553: E/AndroidRuntime(5001): java.lang.IllegalStateException: Could not execute method of the activity 08-14 22:10:44.553: E/AndroidRuntime(5001): at android.view.View$1.onClick(View.java:3823) 08-14 22:10:44.553: E/AndroidRuntime(5001): at android.view.View.performClick(View.java:4438) 08-14 22:10:44.553: E/AndroidRuntime(5001): at android.view.View$PerformClick.run(View.java:18422) 08-14 22:10:44.553: E/AndroidRuntime(5001): at android.os.Handler.handleCallback(Handler.java:733) 08-14 22:10:44.553: E/AndroidRuntime(5001): at android.os.Handler.dispatchMessage(Handler.java:95) 08-14 22:10:44.553: E/AndroidRuntime(5001): at android.os.Looper.loop(Looper.java:136) 08-14 22:10:44.553: E/AndroidRuntime(5001): at 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): Caused by: 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): at android.view.View$1.onClick(View.java:3818) 08-14 22:10:44.553: E/AndroidRuntime(5001): ... 11 more 08-14 22:10:44.553: E/AndroidRuntime(5001): Caused by: android.os.NetworkOnMainThreadException 08-14 22:10:44.553: E/AndroidRuntime(5001): at 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 more

0

1 Answer 1

1

Your crash has nothing to do with the server side code -- you're trying to execute a network call on the main (UI) thread, thus the NetworkOnMainThreadException. Try running it inside an AsyncTask's doInBackground.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.