HttpURLConnection throws an exception
An error occurs in the Http URL link, mainly becauseMain UI thread, UseNetwork call), ThrowNetworkOnMainThreadExceptionException.
Version:API level 11Above.
You can use oneSimple thread, InBackend (asynctask) CallProgram to avoid this situation;
OtherwiseGetResponseCode ()The function throws an exception.
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 the program that requires network call
Thread).