HTTP URL link, an error occurs, mainly because in the main UI thread, the Networkonmainthreadexception exception is thrown by using network invoke (network call).
Version: API level above 11.
You can avoid this situation by using a simple thread that invokes the program in the background (asynctask);
Otherwise, the exception is thrown at the Getresponsecode () function.
Android api:http://developer.android.com/reference/android/os/asynctask.html.
Code:
String quakefeed = getString (r.string.quake_feed);
url = new URL (quakefeed);
URLConnection connection;
Connection = Url.openconnection ();
HttpURLConnection httpconnection = (httpurlconnection) connection;
int responsecode = Httpconnection.getresponsecode ();
Modify:
Thread t = new Thread (new Runnable () {
@Override public
void Run () {
refreshearthquakes ();
}
});
T.start ();
That is, put a program that requires network calls (network call) into threads to execute.
Author: csdn Blog spike_king
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/extra/