HTTP响应总是使用Android导致I/OEException
下面的代码总是在我检查浏览器中的URI时给出I/O EXCPITION,它的工作和I Internet权限在Android清单文件中给出。我是这样从点击按钮调用的HTTP响应总是使用Android导致I/OEException,android,Android,下面的代码总是在我检查浏览器中的URI时给出I/O EXCPITION,它的工作和I Internet权限在Android清单文件中给出。我是这样从点击按钮调用的 CheckMetaData cmd=new CheckMetaData(); cmd.execute(url); ---------类别定义-- 私有类CheckMetaData扩展异步任务{ 受保护的InputStream doInBackground(字符串…参数){ 字符串url=params[0]; DefaultHttpCl
CheckMetaData cmd=new CheckMetaData();
cmd.execute(url);
---------类别定义--
私有类CheckMetaData扩展异步任务{
受保护的InputStream doInBackground(字符串…参数){
字符串url=params[0];
DefaultHttpClient=新的DefaultHttpClient();
HttpGet getRequest=新的HttpGet(url);
试一试{
HttpResponse getResponse=client.execute(getRequest);
final int statusCode=getResponse.getStatusLine().getStatusCode();
if(statusCode!=HttpStatus.SC\u OK){
Log.w(getClass().getSimpleName(),
URL+URL的“错误”+statusCode+);
返回null;
}
HttpEntity getResponseEntity=getResponse.getEntity();
返回getResponseEntity.getContent();
}
捕获(IOE异常){
getRequest.abort();
Log.w(getClass().getSimpleName(),“URL错误”+URL,e);
}
返回null;
}
在catch
块中使用e.printStackTrace()
并检查logcat以查看有关失败原因的更多详细信息。08-03 16:46:22.293:e/AndroidRuntime(787):android.os.NetworkOnMainThreadException但为什么?我使用的是Asynctask,而此活动是从以前的活动启动的,如受保护的void onPostExecute(char[]result){…JSONObject vehicle=new JSONObject(new String(result));Intent available=new Intent();available.setClass(DattabActivity.this,Available_Surveys.class);Available.putExtra(“userid”,vehicle.getString(“SurveyorId”);startActivity(Available);我唯一的猜测是您正在返回带有此行的InputStream
。返回getResponseEntity.getContent()
。这意味着InputStream
作为主/UI线程上运行的onPostExecute(…)
方法的结果返回给onPostExecute(…)
方法。在doInBackground(…)
方法中处理InputStream
,这可能会解决问题。
CheckMetaData cmd = new CheckMetaData();
cmd.execute(url);
---------class def ---
private class CheckMetaData extends AsyncTask<String,Integer,InputStream> {
protected InputStream doInBackground(String... params) {
String url=params[0];
DefaultHttpClient client = new DefaultHttpClient();
HttpGet getRequest = new HttpGet(url);
try {
HttpResponse getResponse = client.execute(getRequest);
final int statusCode = getResponse.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
Log.w(getClass().getSimpleName(),
"Error " + statusCode + " for URL " + url);
return null;
}
HttpEntity getResponseEntity = getResponse.getEntity();
return getResponseEntity.getContent();
}
catch (IOException e) {
getRequest.abort();
Log.w(getClass().getSimpleName(), "Error for URL " + url, e);
}
return null;
}