安卓开发时在service中创建一个线程,在新线程中通过http访问网络时出现异常,请问如何能正常访问网络? 20
如果在主线程中访问网络正常,通过service的新线程访问时就异常,代码如下,异常发生在httpResponse=httpClient.execute(httpGet);...
如果在主线程中访问网络正常,通过service的新线程访问时就异常,代码如下,异常发生在httpResponse = httpClient.execute(httpGet); 请高手给与解答,重分答谢!
public void run() {
// TODO Auto-generated method stub
String forecastUrl = "http://m.weather.com.cn/data/101070201.html";
//生成一个请求对象
HttpGet httpGet = new HttpGet(forecastUrl);
//生成一个Http客户端对象
HttpClient httpClient = new DefaultHttpClient();
//使用Http客户端发送请求对象
InputStream inputStream = null;
try {
httpResponse = httpClient.execute(httpGet);
httpEntity = httpResponse.getEntity();
inputStream = httpEntity.getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String result = "";
String line = "";
while((line = reader.readLine()) != null){
result = result + line;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
原来是模拟器问题,已经解决 展开
public void run() {
// TODO Auto-generated method stub
String forecastUrl = "http://m.weather.com.cn/data/101070201.html";
//生成一个请求对象
HttpGet httpGet = new HttpGet(forecastUrl);
//生成一个Http客户端对象
HttpClient httpClient = new DefaultHttpClient();
//使用Http客户端发送请求对象
InputStream inputStream = null;
try {
httpResponse = httpClient.execute(httpGet);
httpEntity = httpResponse.getEntity();
inputStream = httpEntity.getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String result = "";
String line = "";
while((line = reader.readLine()) != null){
result = result + line;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
原来是模拟器问题,已经解决 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询