Advertisement
Guest User

Untitled

a guest
May 5th, 2015
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. class RequestTask extends AsyncTask<String, String, String>{
  2.  
  3. @Override
  4. protected String doInBackground(String... uri) {
  5. HttpClient httpclient = new DefaultHttpClient();
  6. HttpResponse response;
  7. String responseString = null;
  8. try {
  9. response = httpclient.execute(new HttpGet(uri[0]));
  10. StatusLine statusLine = response.getStatusLine();
  11. if(statusLine.getStatusCode() == HttpStatus.SC_OK){
  12. ByteArrayOutputStream out = new ByteArrayOutputStream();
  13. response.getEntity().writeTo(out);
  14. responseString = out.toString();
  15. out.close();
  16. } else{
  17. //Closes the connection.
  18. response.getEntity().getContent().close();
  19. throw new IOException(statusLine.getReasonPhrase());
  20. }
  21. } catch (ClientProtocolException e) {
  22. //TODO Handle problems..
  23. } catch (IOException e) {
  24. //TODO Handle problems..
  25. }
  26. return responseString;
  27. }
  28.  
  29. @Override
  30. protected void onPostExecute(String result) {
  31. super.onPostExecute(result);
  32. ShowMsg(thisone,result);
  33. }
  34. }
  35.  
  36. new RequestTask().execute("http://www.stackoverflow.com");
  37.  
  38. new RequestTask().execute("http://127.0.0.1");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement