Advertisement
Limo

httpclient

Jun 22nd, 2013
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. /***
  2. Copyright (c) 2009
  3. Author: Stefan Klumpp <[email protected]>
  4. Web: http://stefanklumpp.com
  5.  
  6. Licensed under the Apache License, Version 2.0 (the "License"); you may
  7. not use this file except in compliance with the License. You may obtain
  8. a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. */
  16.  
  17. package com.devstream.http;
  18.  
  19. import java.io.BufferedReader;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import java.io.InputStreamReader;
  23. import java.io.UnsupportedEncodingException;
  24. import java.util.zip.GZIPInputStream;
  25. import org.apache.http.Header;
  26. import org.apache.http.HttpEntity;
  27. import org.apache.http.HttpResponse;
  28. import org.apache.http.client.methods.HttpPost;
  29. import org.apache.http.entity.StringEntity;
  30. import org.apache.http.impl.client.DefaultHttpClient;
  31. import org.apache.http.message.BasicHeader;
  32. import org.apache.http.protocol.HTTP;
  33. import org.json.JSONObject;
  34. import android.util.Log;
  35.  
  36. public class HttpClient {
  37. private static final String TAG = "HttpClient";
  38.  
  39. public static JSONObject SendHttpPost(String URL, JSONObject jsonObjSend) {
  40.  
  41. try {
  42. DefaultHttpClient httpclient = new DefaultHttpClient();
  43. HttpPost httpPostRequest = new HttpPost(URL);
  44.  
  45. StringEntity se;
  46. se = new StringEntity(jsonObjSend.toString());
  47.  
  48. // Set HTTP parameters
  49. httpPostRequest.setEntity(se);
  50. httpPostRequest.setHeader("Accept", "application/json");
  51. se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
  52. httpPostRequest.setHeader("Content-type", "application/json");
  53. httpPostRequest.setHeader("Accept-Encoding", "gzip"); // only set this parameter if you would like to use gzip compression
  54.  
  55. long t = System.currentTimeMillis();
  56. HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest);
  57. Log.i(TAG, "HTTPResponse received in [" + (System.currentTimeMillis()-t) + "ms]");
  58.  
  59. // Get hold of the response entity (-> the data):
  60. HttpEntity entity = response.getEntity();
  61.  
  62. if (entity != null) {
  63. // Read the content stream
  64. InputStream instream = entity.getContent();
  65. Header contentEncoding = response.getFirstHeader("Content-Encoding");
  66. if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
  67. instream = new GZIPInputStream(instream);
  68. }
  69.  
  70. // convert content stream to a String
  71. String resultString= convertStreamToString(instream);
  72. instream.close();
  73. resultString = resultString.substring(1,resultString.length()-1); // remove wrapping "[" and "]"
  74.  
  75. // Transform the String into a JSONObject
  76. JSONObject jsonObjRecv = new JSONObject(resultString);
  77. // Raw DEBUG output of our received JSON object:
  78. Log.i(TAG,"<JSONObject>\n"+jsonObjRecv.toString()+"\n</JSONObject>");
  79.  
  80. return jsonObjRecv;
  81. }
  82.  
  83. }
  84. catch (Exception e)
  85. {
  86. // More about HTTP exception handling in another tutorial.
  87. // For now we just print the stack trace.
  88. e.printStackTrace();
  89. }
  90. return null;
  91. }
  92.  
  93.  
  94. private static String convertStreamToString(InputStream is) {
  95. /*
  96. * To convert the InputStream to String we use the BufferedReader.readLine()
  97. * method. We iterate until the BufferedReader return null which means
  98. * there's no more data to read. Each line will appended to a StringBuilder
  99. * and returned as String.
  100. *
  101. * (c) public domain: http://senior.ceng.metu.edu.tr/2009/praeda/2009/01/11/a-simple-restful-client-at-android/
  102. */
  103. BufferedReader reader = null;
  104. try {
  105. reader = new BufferedReader(new InputStreamReader(is,"utf-8"), 8);
  106. } catch (UnsupportedEncodingException e1) {
  107. // TODO Auto-generated catch block
  108. e1.printStackTrace();
  109. }
  110. StringBuilder sb = new StringBuilder();
  111.  
  112. String line = null;
  113. try {
  114. while ((line = reader.readLine()) != null) {
  115. sb.append(line + "\n");
  116. }
  117. } catch (IOException e) {
  118. e.printStackTrace();
  119. } finally {
  120. try {
  121. is.close();
  122. } catch (IOException e) {
  123. e.printStackTrace();
  124. }
  125. }
  126. return sb.toString();
  127. }
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement