Skip to content

Instantly share code, notes, and snippets.

@haldun
Created February 15, 2015 04:40
Show Gist options
  • Save haldun/2903c3a13844903f0221 to your computer and use it in GitHub Desktop.
Save haldun/2903c3a13844903f0221 to your computer and use it in GitHub Desktop.
package fazlamesai.com.myapplication;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final String[] urls = {
"http://www.yahoo.com",
"http://developer.android.com/reference/android/os/AsyncTask.html",
"http://www.google.de",
"http://www.apple.fr",
};
for (final String url : urls) {
new AsyncTask<String, Void, Void>() {
@Override
protected Void doInBackground(String... params) {
final String urlString = params[0];
Log.i("HTTP", "Will fetch " + urlString);
try {
final URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream inputStream = new BufferedInputStream(connection.getInputStream());
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
int responseCode = connection.getResponseCode();
Log.i("HTTP", "Got " + responseCode + " for url " + url);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, url);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment