HttpResponse httpResponse = httpClient.execute(httpRequest);io异常
代码:主线程getWeatherDate中publicStringconnServerForResult(StringstrUrl){//获取HttpGet对象HttpG...
代码:
主线程getWeatherDate中
public String connServerForResult(String strUrl) {
// 获取HttpGet对象
HttpGet httpRequest = new HttpGet(strUrl);
String strResult = "";
try {
// HttpClient对象
HttpClient httpClient = new DefaultHttpClient();
// 获得HttpResponse对象
HttpResponse httpResponse = httpClient.execute(httpRequest);//io异常
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 取得返回的数据
strResult = EntityUtils.toString(httpResponse.getEntity());
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("rresult" + strResult);
return strResult; // 返回结果
}
异步:
class MenuTask extends AsyncTask<Integer, Integer, Integer> {
@Override
protected Integer doInBackground(Integer... i) {// 处理后台执行的任务,在后台线程执行
for(Map<String,String> id:cityandId){
addressId =id.get("id");
getWeatherDate data = new getWeatherDate();
String jsonStr=data.getJSONString(addressId);
data.setWeatherBean(jsonStr);
weatherBeanList.add(data.getWeatherBean());
}
return 0;
}
@Override
protected void onPostExecute(Integer result) {// 后台任务执行完之后被调用,在ui线程执行
}
}
url debug时正常 其路径可以在浏览器中显示出json数据
请问应该怎么改
异步的代码贴错了……
上面那个方法是Util类中的( 自己写的)
class MenuTask extends AsyncTask<Integer,Integer,Integer> {
@Override
protected Integer doInBackground(Integer... i) {//处理后台执行的任务,在后台线程执行
Util data = new Util();
s=data.connServerForResult("http://m.weather.com.cn/data/101190101.html");
return 0;
}
} 展开
主线程getWeatherDate中
public String connServerForResult(String strUrl) {
// 获取HttpGet对象
HttpGet httpRequest = new HttpGet(strUrl);
String strResult = "";
try {
// HttpClient对象
HttpClient httpClient = new DefaultHttpClient();
// 获得HttpResponse对象
HttpResponse httpResponse = httpClient.execute(httpRequest);//io异常
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 取得返回的数据
strResult = EntityUtils.toString(httpResponse.getEntity());
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("rresult" + strResult);
return strResult; // 返回结果
}
异步:
class MenuTask extends AsyncTask<Integer, Integer, Integer> {
@Override
protected Integer doInBackground(Integer... i) {// 处理后台执行的任务,在后台线程执行
for(Map<String,String> id:cityandId){
addressId =id.get("id");
getWeatherDate data = new getWeatherDate();
String jsonStr=data.getJSONString(addressId);
data.setWeatherBean(jsonStr);
weatherBeanList.add(data.getWeatherBean());
}
return 0;
}
@Override
protected void onPostExecute(Integer result) {// 后台任务执行完之后被调用,在ui线程执行
}
}
url debug时正常 其路径可以在浏览器中显示出json数据
请问应该怎么改
异步的代码贴错了……
上面那个方法是Util类中的( 自己写的)
class MenuTask extends AsyncTask<Integer,Integer,Integer> {
@Override
protected Integer doInBackground(Integer... i) {//处理后台执行的任务,在后台线程执行
Util data = new Util();
s=data.connServerForResult("http://m.weather.com.cn/data/101190101.html");
return 0;
}
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询