Skip to content

Instantly share code, notes, and snippets.

@eneim
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eneim/59df18b4f92065e7d409 to your computer and use it in GitHub Desktop.
Save eneim/59df18b4f92065e7d409 to your computer and use it in GitHub Desktop.
private class DownloadTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
HttpResponse response = null;
HttpGet httpGet = null;
HttpClient mHttpClient = null;
String s = "";
// check if the url is invalid
if (urls == null || urls.length == 0)
return null;
try {
if(mHttpClient == null) {
mHttpClient = new DefaultHttpClient();
}
httpGet = new HttpGet(urls[0]);
response = mHttpClient.execute(httpGet);
s = EntityUtils.toString(response.getEntity(), "UTF-8");
} catch (IOException e) {
e.printStackTrace();
}
return s;
}
@Override
protected void onPostExecute(String... strings) {
// show your dialog here
// please do check null first
if (strings == null || strings.length == 0)
return;
new AlertDialog.Builder(PrintDemo.this)
.setTitle("Message")
.setMessage(strings[0])
.setPositiveButton(android.R.string.ok, null)
.setCancelable(false)
.create()
.show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment