Java 应用程序在http请求时崩溃
我在安卓系统里是个十足的傻瓜! 但我正试图找出如何将android与MySql数据库连接起来。 我发现的方法是对连接到数据库的php文件执行Http请求。但是当我执行httpPost请求时,应用程序崩溃!!(不幸的是,应用程序…已停止!) 下面是我的方法代码: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
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();