标签:
//清单文件中添加权限
<uses-permission android:name="android.permission.INTERNET"/>
new Thread(){
public void run() {
try {
URL url=new URL(urlPath);
HttpURLConnection urlConnection=(HttpURLConnection) url.openConnection();
urlConnection.setConnectTimeout(5000);
urlConnection.setReadTimeout(5000);
urlConnection.setRequestMethod("GET");
urlConnection.connect();
int code=urlConnection.getResponseCode();
Log.i("44444444444444", "code="+code);
if (code==200) {
InputStream inputStream=urlConnection.getInputStream();
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
String line;
StringBuffer buffer=new StringBuffer();
while ((line=bufferedReader.readLine())!=null) {
buffer.append(line);
}
String str=buffer.toString();
Message message=new Message();
message.what=0;
message.obj=str;
//发送Handler消息
handler.sendMessage(message);
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
}.start();
//在UI主线程中创建Handler接收
private Handler handler=new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 0:
//清空集合
news.clear();
//刷新适配器
adapter.notifyDataSetChanged();
String text=(String) msg.obj;
//解析XML文件
toXml(text);
break;
default:
break;
}
}
};
标签:
原文地址:http://www.cnblogs.com/wushanmanong/p/5809632.html