-1

i'm a totally noob in android! But i am trying to find out how to connect android with a MySql database. The way i found out is to do Http requests to an php file which is connected to the database. But when i do a httpPost request the app crashes!! (unfortunately the app ... has stoped!)

Here my code of the method:

public void makeHTTPRequest(View v) {
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost("http://www.babyschuh.de/malte/androidApp/getAllDaten.php");        //URL eintragen!!!

    List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
    nameValuePair.add(new BasicNameValuePair("name", "Malte Schlichting"));
    nameValuePair.add(new BasicNameValuePair("raum", "12"));

    // Encoding the POST parameters
    try {
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    // Making fHTTP Request
    try {
        HttpResponse response = httpClient.execute(httpPost);

        Log.d("Http Response", response.toString());
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch ( IOException e) {
        e.printStackTrace();
    }
}

And the logcat:

11-25 15:51:09.021  10114-10114/? D/dalvikvm﹕ Not late-enabling CheckJNI (already on)
11-25 15:51:10.412  10114-10114/com.malte.httprequests E/Trace﹕ error opening trace file: No such file or directory (2)
11-25 15:51:10.751  10114-10114/com.malte.httprequests W/dalvikvm﹕ VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
11-25 15:51:10.762  10114-10114/com.malte.httprequests I/dalvikvm﹕ Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.internal.view.WindowCallbackWrapper.onSearchRequested
11-25 15:51:10.762  10114-10114/com.malte.httprequests W/dalvikvm﹕ VFY: unable to resolve interface method 14064: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
11-25 15:51:10.781  10114-10114/com.malte.httprequests D/dalvikvm﹕ VFY: replacing opcode 0x72 at 0x0002
11-25 15:51:10.781  10114-10114/com.malte.httprequests I/dalvikvm﹕ Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.internal.view.WindowCallbackWrapper.onWindowStartingActionMode
11-25 15:51:10.791  10114-10114/com.malte.httprequests W/dalvikvm﹕ VFY: unable to resolve interface method 14068: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
11-25 15:51:10.791  10114-10114/com.malte.httprequests D/dalvikvm﹕ VFY: replacing opcode 0x72 at 0x0002
11-25 15:51:11.131  10114-10114/com.malte.httprequests I/dalvikvm﹕ Could not find method android.view.ViewGroup.onRtlPropertiesChanged, referenced from method android.support.v7.widget.Toolbar.onRtlPropertiesChanged
11-25 15:51:11.131  10114-10114/com.malte.httprequests W/dalvikvm﹕ VFY: unable to resolve virtual method 13965: Landroid/view/ViewGroup;.onRtlPropertiesChanged (I)V
11-25 15:51:11.131  10114-10114/com.malte.httprequests D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0007
11-25 15:51:11.151  10114-10114/com.malte.httprequests I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
11-25 15:51:11.161  10114-10114/com.malte.httprequests W/dalvikvm﹕ VFY: unable to resolve virtual method 408: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
11-25 15:51:11.171  10114-10114/com.malte.httprequests D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
11-25 15:51:11.171  10114-10114/com.malte.httprequests I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
11-25 15:51:11.171  10114-10114/com.malte.httprequests W/dalvikvm﹕ VFY: unable to resolve virtual method 430: Landroid/content/res/TypedArray;.getType (I)I
11-25 15:51:11.181  10114-10114/com.malte.httprequests D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
11-25 15:51:11.311  10114-10118/com.malte.httprequests D/dalvikvm﹕ GC_CONCURRENT freed 214K, 2% free 18950K/19271K, paused 43ms+6ms, total 111ms
11-25 15:51:11.311  10114-10114/com.malte.httprequests D/dalvikvm﹕ WAIT_FOR_CONCURRENT_GC blocked 17ms
11-25 15:51:11.461  10114-10114/com.malte.httprequests D/libEGL﹕ loaded /system/lib/egl/libEGL_emulation.so
11-25 15:51:11.461  10114-10114/com.malte.httprequests D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_emulation.so
11-25 15:51:11.481  10114-10114/com.malte.httprequests D/libEGL﹕ loaded /system/lib/egl/libGLESv2_emulation.so
11-25 15:51:11.492  10114-10114/com.malte.httprequests D/﹕ HostConnection::get() New Host Connection established 0x2a080d78, tid 10114
11-25 15:51:11.551  10114-10114/com.malte.httprequests W/EGL_emulation﹕ eglSurfaceAttrib not implemented
11-25 15:51:11.581  10114-10114/com.malte.httprequests D/OpenGLRenderer﹕ Enabling debug mode 0
11-25 15:51:15.222  10114-10114/com.malte.httprequests D/AndroidRuntime﹕ Shutting down VM
11-25 15:51:15.222  10114-10114/com.malte.httprequests W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40a122a0)
11-25 15:51:15.262  10114-10114/com.malte.httprequests E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View$1.onClick(View.java:3591)
            at android.view.View.performClick(View.java:4084)
            at android.view.View$PerformClick.run(View.java:16966)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at android.view.View$1.onClick(View.java:3586)
            at android.view.View.performClick(View.java:4084)
            at android.view.View$PerformClick.run(View.java:16966)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.os.NetworkOnMainThreadException
            at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
            at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
            at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
            at java.net.InetAddress.getAllByName(InetAddress.java:214)
            at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
            at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
            at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
            at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
            at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
            at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
            at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
            at com.malte.httprequests.MainActivity.makeHTTPRequest(MainActivity.java:48)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at android.view.View$1.onClick(View.java:3586)
            at android.view.View.performClick(View.java:4084)
            at android.view.View$PerformClick.run(View.java:16966)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
11-25 15:51:20.231  10114-10114/com.malte.httprequests I/Process﹕ Sending signal. PID: 10114 SIG: 9

I just don't know what to do!! :/ But when i did a request to fetch JSONArray using the Volley class everything worked fine.

So my question are: Why the upper example doesn't work? And if there is an easier way to connect android to mysql database?

PS: -min. android SDK: 16 -Adroid Studio 1.3.2 -yes, i insert internet permission

PPS: I tried different example apps but nothing worked.

jabu.10245
  • 1,884
  • 1
  • 11
  • 20
Malte Sli
  • 71
  • 1
  • 9

1 Answers1

0

Create an Asynctask:

private class NetworkTask extends AsyncTask<Void, Void, Void>{
         @Override
         protected Void doInBackground(Void... arg0) {
             HttpClient httpClient = new DefaultHttpClient();
             HttpPost httpPost = new HttpPost("http://www.babyschuh.de/malte/androidApp/getAllDaten.php");        //URL eintragen!!!

             List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
             nameValuePair.add(new BasicNameValuePair("name", "Malte Schlichting"));
             nameValuePair.add(new BasicNameValuePair("raum", "12"));

             // Encoding the POST parameters
             try {
                 httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
             } catch (UnsupportedEncodingException e) {
                 e.printStackTrace();
             }

             // Making fHTTP Request
             try {
                 HttpResponse response = httpClient.execute(httpPost);

                 Log.d("Http Response", response.toString());
             } catch (ClientProtocolException e) {
                 e.printStackTrace();
             } catch ( IOException e) {
                 e.printStackTrace();
             }
         }
 }

And start this Task by calling

new NetworkTask().execute();

You Exception means that you are not allowed to do any network-related stuff on the main thread. The reason is that your UI does not respond while your app communicates with the server if you do those operations on the main thread.

Jörn Buitink
  • 2,906
  • 2
  • 22
  • 33