Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 应用程序在http请求时崩溃_Java_Android_Http_Android Asynctask - Fatal编程技术网

Java 应用程序在http请求时崩溃

Java 应用程序在http请求时崩溃,java,android,http,android-asynctask,Java,Android,Http,Android Asynctask,我在安卓系统里是个十足的傻瓜! 但我正试图找出如何将android与MySql数据库连接起来。 我发现的方法是对连接到数据库的php文件执行Http请求。但是当我执行httpPost请求时,应用程序崩溃!!(不幸的是,应用程序…已停止!) 下面是我的方法代码: public void makeHTTPRequest(View v) { HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new

我在安卓系统里是个十足的傻瓜! 但我正试图找出如何将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();
    }
}
我只是不知道该怎么办!!:/ 但是当我使用截击类请求获取JSONArray时,一切都很好

因此,我的问题是: 为什么上面的例子不起作用? 还有,是否有更简单的方法将android连接到mysql数据库

PS:-最小安卓SDK:16 -Adroid Studio 1.3.2 -是的,我插入了互联网许可

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

创建一个异步任务:

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();
             }
         }
 }
异常意味着不允许在主线程上执行任何与网络相关的操作。原因是,如果您在主线程上执行这些操作,则应用程序与服务器通信时,用户界面不会响应。

启动一个新线程(例如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();