Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2011
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. package com.javmedapp.Master;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.net.ProtocolException;
  8. import java.net.URI;
  9. import java.net.URISyntaxException;
  10.  
  11. import org.apache.http.HttpEntity;
  12. import org.apache.http.HttpResponse;
  13. import org.apache.http.client.HttpClient;
  14. import org.apache.http.client.methods.HttpGet;
  15. import org.apache.http.impl.client.DefaultHttpClient;
  16.  
  17. import android.util.Log;
  18.  
  19. public class HttpManager {
  20. HttpClient httpClient = new DefaultHttpClient();
  21.  
  22. public String OpenHttpConnection(String urlString) throws IOException,
  23. ProtocolException {
  24. URI uri = null;
  25.  
  26. try {
  27. uri = new URI(urlString.replace(" ", "%20"));
  28. } catch (URISyntaxException e) {
  29. // TODO Auto-generated catch block
  30. e.printStackTrace();
  31. }
  32.  
  33. String jsonString = null;
  34. HttpResponse httpResponse;
  35. // urlString = String.format(urlString);
  36. // Log.v("Encoded URL Request",urlString);
  37. HttpGet get = new HttpGet(uri);
  38. System.out.println("after replacing~~~~~~~~" + uri);
  39. httpResponse = httpClient.execute(get);
  40. Log.d("", httpResponse.getStatusLine().toString());
  41. HttpEntity entity = httpResponse.getEntity();
  42.  
  43. if (entity != null) {
  44. InputStream inputStream = entity.getContent();
  45. jsonString = convertStreamToString(inputStream);
  46.  
  47. inputStream.close();
  48. System.out.println(jsonString.toString());
  49. Log.v("Json Response", jsonString + jsonString.length());
  50.  
  51. }
  52. return jsonString;
  53. }
  54.  
  55. private String convertStreamToString(InputStream is) {
  56. BufferedReader reader = new BufferedReader(new InputStreamReader(is),
  57. 8192);
  58. StringBuilder sb = new StringBuilder();
  59.  
  60. String line = null;
  61. try {
  62. while ((line = reader.readLine()) != null) {
  63. sb.append(line + "\n");
  64. }
  65. } catch (IOException e) {
  66. Log.e("Convert Input Stream",
  67. "Caught IOException in convertStreamToString()", e);
  68. return null;
  69. } finally {
  70. try {
  71. is.close();
  72. } catch (IOException e) {
  73. Log.e("Convert Input Stream",
  74. "Caught IOException in convertStreamToString()", e);
  75. return null;
  76. }
  77. }
  78.  
  79. return sb.toString();
  80. }
  81.  
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement