Advertisement
ziedrebhi

ServiceHandler.java

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