Add permissions to the manifest file
<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;
Send Handler message
Handler.sendmessage (message);
}
} catch (malformedurlexception E) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (ioexception E) {
TODO auto-generated Catch block
E.printstacktrace ();
}
};
}.start ();
Create a handler receive in the main thread of the UI
Private Handler handler=new Handler () {
public void Handlemessage (android.os.Message Msg) {
Switch (msg.what) {
Case 0:
Emptying the collection
News.clear ();
Refresh Adapter
Adapter.notifydatasetchanged ();
String text= (string) msg.obj;
Parsing XML files
TOXML (text);
Break
Default
Break
}
}
};
HttpURLConnection request for a GET request for network data