对新手来说莫过于一个很大的坑了,遇到这个问题,你会发现,ip正确,给了访问网络的权限,buffer = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));始终会是个空对象,最后终于找到答案:从Honeycomb SDK(3.0)开始,google不再允许网络请求(HTTP、Socket)等相关操作直接在Main Thread类中,,,,,
修改方法1:
在setContentView(R.layout.activity_main)下添加:
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
方法2:新开线程
new Thread(downloadRun).start();
}
/**
* 下载线程
*/
Runnable downloadRun = new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
updateListView();
}
};