Advertisement
Guest User

Untitled

a guest
Aug 1st, 2014
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. new HttpAsyncTask().execute("http://myweb.com/api/rest/products");
  2. public static String GET(String url){
  3. InputStream inputStream = null;
  4. String result = "";
  5. try {
  6.  
  7. // create HttpClient
  8. HttpClient httpclient = new DefaultHttpClient();
  9.  
  10. // make GET request to the given URL
  11. HttpResponse httpResponse = httpclient.execute(new HttpGet(url));
  12.  
  13. // receive response as inputStream
  14. inputStream = httpResponse.getEntity().getContent();
  15.  
  16. // convert inputstream to string
  17. if(inputStream != null)
  18. result = convertInputStreamToString(inputStream);
  19. else
  20. result = "Did not work!";
  21.  
  22. } catch (Exception e) {
  23. Log.d("InputStream", e.getLocalizedMessage());
  24. }
  25.  
  26. return result;
  27. }
  28.  
  29. private static String convertInputStreamToString(InputStream inputStream) throws IOException{
  30. BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
  31. String line = "";
  32. String result = "";
  33. while((line = bufferedReader.readLine()) != null)
  34. result += line;
  35.  
  36. inputStream.close();
  37. return result;
  38.  
  39. }
  40.  
  41. private class HttpAsyncTask extends AsyncTask<String, Void, String> {
  42. @Override
  43. protected String doInBackground(String... urls) {
  44.  
  45. return GET(urls[0]);
  46. }
  47. // onPostExecute displays the results of the AsyncTask.
  48. @Override
  49. protected void onPostExecute(String result) {
  50. Toast.makeText(getBaseContext(), "Received!", Toast.LENGTH_LONG).show();
  51. etResponse.setText(result);
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement