Advertisement
Guest User

Untitled

a guest
Jun 29th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.25 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.InputStreamReader;
  5. import java.io.UnsupportedEncodingException;
  6. import java.util.List;
  7.  
  8. import org.apache.http.HttpEntity;
  9. import org.apache.http.HttpResponse;
  10. import org.apache.http.NameValuePair;
  11. import org.apache.http.client.ClientProtocolException;
  12. import org.apache.http.client.entity.UrlEncodedFormEntity;
  13. import org.apache.http.client.methods.HttpGet;
  14. import org.apache.http.client.methods.HttpPost;
  15. import org.apache.http.client.utils.URLEncodedUtils;
  16. import org.apache.http.impl.client.DefaultHttpClient;
  17.  
  18. import android.util.Log;
  19.  
  20. public class ServiceHandler {
  21.  
  22.     static InputStream is = null;
  23.     static String response = null;
  24.     public final static int GET = 1;
  25.     public final static int POST = 2;
  26.  
  27.     public ServiceHandler() {
  28.  
  29.     }
  30.  
  31.     /**
  32.      * Making service call
  33.      * @url - url to make request
  34.      * @method - http request method
  35.      * */
  36.     public String makeServiceCall(String url, int method) {
  37.         return this.makeServiceCall(url, method, null);
  38.     }
  39.  
  40.     /**
  41.      * Making service call
  42.      * @url - url to make request
  43.      * @method - http request method
  44.      * @params - http request params
  45.      * */
  46.     public String makeServiceCall(String url, int method,
  47.                                   List<NameValuePair> params) {
  48.         try {
  49.             // http client
  50.             DefaultHttpClient httpClient = new DefaultHttpClient();
  51.             HttpEntity httpEntity = null;
  52.             HttpResponse httpResponse = null;
  53.  
  54.             // Checking http request method type
  55.             if (method == POST) {
  56.                 HttpPost httpPost = new HttpPost(url);
  57.                 // adding post params
  58.                 if (params != null) {
  59.                     httpPost.setEntity(new UrlEncodedFormEntity(params));
  60.                 }
  61.  
  62.                 httpResponse = httpClient.execute(httpPost);
  63.  
  64.             } else if (method == GET) {
  65.                 // appending params to url
  66.                 if (params != null) {
  67.                     String paramString = URLEncodedUtils
  68.                             .format(params, "utf-8");
  69.                     url += "?" + paramString;
  70.                 }
  71.                 HttpGet httpGet = new HttpGet(url);
  72.  
  73.                 httpResponse = httpClient.execute(httpGet);
  74.  
  75.             }
  76.             httpEntity = httpResponse.getEntity();
  77.             is = httpEntity.getContent();
  78.  
  79.         } catch (UnsupportedEncodingException e) {
  80.             e.printStackTrace();
  81.         } catch (ClientProtocolException e) {
  82.             e.printStackTrace();
  83.         } catch (IOException e) {
  84.             e.printStackTrace();
  85.         }
  86.  
  87.         try {
  88.             BufferedReader reader = new BufferedReader(new InputStreamReader(
  89.                     is, "UTF-8"), 8);
  90.             StringBuilder sb = new StringBuilder();
  91.             String line = null;
  92.             while ((line = reader.readLine()) != null) {
  93.                 sb.append(line + "\n");
  94.             }
  95.             is.close();
  96.             response = sb.toString();
  97.         } catch (Exception e) {
  98.             Log.e("Buffer Error", "Error: " + e.toString());
  99.         }
  100.  
  101.         return response;
  102.  
  103.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement