2015-11-25 190 views
-1

我是一個完全noob在android! 但我想了解如何連接android與MySql數據庫。 我發現的方式是對連接到數據庫的php文件執行Http請求。但是當我做一個httpPost請求應用程序崩潰! (不幸的是,應用程序......已經採空!)應用程序崩潰在http請求

這裏我的方法的代碼:

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(); 
    } 
} 

而且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 

我只是不知道該怎麼辦! ! :/ 但是,當我做了一個使用Volley類獲取JSONArray的請求一切正常。

所以我的問題是: 爲什麼上面的例子不起作用? 如果有更簡單的方法來連接android到mysql數據庫?

PS:-min。 Android SDK:16 -Adroid Studio 1.3.2 -yes,我插入互聯網權限

PPS:我嘗試了不同的示例應用程序,但沒有任何工作。

+2

開始一個新線程(例如AsyncTask)來完成您的網絡資料。 –

+0

查看http://developer.android.com/reference/android/os/AsyncTask.html – Ra1nWarden

回答

0

創建的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(); 
      } 
     } 
} 

,並通過調用

new NetworkTask().execute(); 

您的異常意味着你不能做在主線程上的任何網絡相關的東西開始這個任務。原因是如果您在主線程上執行這些操作,則您的UI在您的應用程序與服務器通信時不會響應。

相關問題