应用程序在http请求

时间:2015-11-25 16:02:55

标签: java android http android-asynctask

我在android中完全是菜鸟! 但我试图找出如何连接Android与MySql数据库。 我发现的方法是对连接到数据库的php文件执行Http请求。但当我做一个httpPost请求时,应用程序崩溃! (不幸的是,应用......已经停止了!)

这是我的方法代码:

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 - 是的,我插入了互联网许可

PPS:我尝试了不同的示例应用,但没有任何效果。

1 个答案:

答案 0 :(得分: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();

You Exception意味着您不允许在主线程上执行任何与网络相关的操作。原因是,如果您在主线程上执行这些操作,则当您的应用与服务器通信时,您的UI不会响应。