Android HTTPClient导致应用程序崩溃
我有这个安卓代码:Android HTTPClient导致应用程序崩溃,android,Android,我有这个安卓代码: package com.XXX import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.ResponseHandler; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.clie
package com.XXX
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import java.io.IOException;
public class DataProvider {
String baseUri;
DataProvider() {
baseUri = "http://www.XXX.de/XXX/";
}
public String requestUser(String userName) {
HttpClient httpclient = new DefaultHttpClient();
HttpGet request = new HttpGet(baseUri + "get.php?userName=" + userName);
ResponseHandler<String> handler = new BasicResponseHandler();
String result = "";
try {
result = httpclient.execute(request, handler);
} catch (ClientProtocolException e) {
result = "ClientProtocolException was thrown";
} catch (IOException e) {
result = "IOException was thrown";
}
finally {
httpclient.getConnectionManager().shutdown();
}
return result;
}
}
单击菜单项会导致应用程序崩溃。对象browser
和dataProvider
的声明是100%正确的。我在我的设备上测试应用程序,因为我的电脑太弱,无法运行模拟器,因此没有控制台输出
有什么想法吗?:…我的第一印象是HTTP请求的错误声明。在Android中,如果你想做一个HttpRequest,你必须把它放在其他线程中,而不是主线程中 您的
DataProvider.java
在声明和空检查方面是正常的,但不符合安卓Android.os.NetworkOnMainThreadException的安全标准之一
使用AsyncTask运行yout HTTP Request,类似于活动中的内容:
// The other part of your code ....
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_reload_package) {
new DataProviderAsyncTask().execute();
return true;
}
return super.onOptionsItemSelected(item);
}
class DataProviderAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String response = dataProvider
.requestUser("BKDJSHDD-1912772-DIKDS-19172");
return response;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
browser.loadDataWithBaseURL("file:///android_asset/", result,
"text/html", "UTF-8", "file:///android_asset/index.html");
}
}
//代码的另一部分。。。。
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//处理操作栏项目单击此处。操作栏将
//自动处理Home/Up按钮上的点击,只要
//在AndroidManifest.xml中指定父活动时。
int id=item.getItemId();
//noinspection SimplifiableIf语句
if(id==R.id.action\u重载\u包){
新建DataProviderAsyncTask().execute();
返回true;
}
返回super.onOptionsItemSelected(项目);
}
类DataProviderAsyncTask扩展了AsyncTask{
@凌驾
受保护的字符串doInBackground(字符串…参数){
字符串响应=数据提供程序
.requestUser(“BKDJSHDD-1912772-DIKDS-19172”);
返回响应;
}
@凌驾
受保护的void onPostExecute(字符串结果){
super.onPostExecute(结果);
browser.loadDataWithBaseURL(“file:///android_asset/“,结果,
“文本/html”、“UTF-8”file:///android_asset/index.html");
}
}
我希望您提供检查数据提供者的构造函数。有一个拼写错误“因此没有控制台输出”--您可以从硬件和模拟器获得LogCat。事实上,这是相同的过程。根据当前提供的代码,看起来您正在主线程上运行网络请求。是的,但他应该抓住那次撞击,对吗?@commonware谢谢你的提示。
// The other part of your code ....
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_reload_package) {
new DataProviderAsyncTask().execute();
return true;
}
return super.onOptionsItemSelected(item);
}
class DataProviderAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String response = dataProvider
.requestUser("BKDJSHDD-1912772-DIKDS-19172");
return response;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
browser.loadDataWithBaseURL("file:///android_asset/", result,
"text/html", "UTF-8", "file:///android_asset/index.html");
}
}