Created
April 30, 2015 16:43
-
-
Save AdamMc331/d67bf84106b854f26e04 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HttpExampleActivity extends Activity { | |
private static final String DEBUG_TAG = "HttpExample"; | |
private EditText urlText; | |
private TextView textView; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout_main); | |
urlText = (EditText) findViewById(R.id.myUrl); | |
textView = (TextView) findViewById(R.id.myText); | |
Textview text = (TextView) findViewById(R.id.my_text_view); | |
String displayThis = "Display this."; | |
text.setText(displayThis); | |
} | |
// When user clicks button, calls AsyncTask. | |
// Before attempting to fetch the URL, makes sure that there is a network connection. | |
public void myClickHandler(View view) { | |
// Gets the URL from the UI's text field. | |
String stringUrl = urlText.getText().toString(); | |
ConnectivityManager connMgr = (ConnectivityManager) | |
getSystemService(Context.CONNECTIVITY_SERVICE); | |
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); | |
if (networkInfo != null && networkInfo.isConnected()) { | |
new DownloadWebpageTask().execute(stringUrl); | |
} else { | |
textView.setText("No network connection available."); | |
} | |
} | |
// Uses AsyncTask to create a task away from the main UI thread. This task takes a | |
// URL string and uses it to create an HttpUrlConnection. Once the connection | |
// has been established, the AsyncTask downloads the contents of the webpage as | |
// an InputStream. Finally, the InputStream is converted into a string, which is | |
// displayed in the UI by the AsyncTask's onPostExecute method. | |
private class DownloadWebpageTask extends AsyncTask<String, Void, String> { | |
@Override | |
protected String doInBackground(String... urls) { | |
// params comes from the execute() call: params[0] is the url. | |
try { | |
return downloadUrl(urls[0]); | |
} catch (IOException e) { | |
return "Unable to retrieve web page. URL may be invalid."; | |
} | |
} | |
// onPostExecute displays the results of the AsyncTask. | |
@Override | |
protected void onPostExecute(String result) { | |
textView.setText(result); | |
} | |
} | |
// Given a URL, establishes an HttpUrlConnection and retrieves | |
// the web page content as a InputStream, which it returns as | |
// a string. | |
private String downloadUrl(String myurl) throws IOException { | |
InputStream is = null; | |
// Only display the first 500 characters of the retrieved | |
// web page content. | |
int len = 500; | |
try { | |
URL url = new URL("http://i.reddit.com/"); | |
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); | |
conn.setReadTimeout(10000 /* milliseconds */); | |
conn.setConnectTimeout(15000 /* milliseconds */); | |
conn.setRequestMethod("GET"); | |
conn.setDoInput(true); | |
// Starts the query | |
conn.connect(); | |
int response = conn.getResponseCode(); | |
Log.d(DEBUG_TAG, "The response is: " + response); | |
is = conn.getInputStream(); | |
// Convert the InputStream into a string | |
String contentAsString = readIt(is, len); | |
return contentAsString; | |
} finally { | |
if (is != null) { | |
is.close(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment