Advertisement
zza_ibliizt

LoginDatabase CustomHttpClient.java

Dec 19th, 2018
1,282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.53 KB | None | 0 0
  1. package Class;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.net.URI;
  7. import java.util.ArrayList;
  8. import org.apache.http.HttpResponse;
  9. import org.apache.http.NameValuePair;
  10. import org.apache.http.client.HttpClient;
  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.conn.params.ConnManagerParams;
  15. import org.apache.http.impl.client.DefaultHttpClient;
  16. import org.apache.http.params.HttpConnectionParams;
  17. import org.apache.http.params.HttpParams;
  18.  
  19. public class CustomHttpClient {
  20.    
  21.    /** Durasi Timeout */
  22.     public static final int HTTP_TIMEOUT = 10 * 1000; // milliseconds (10 detik)
  23.  
  24.     private static HttpClient mHttpClient;
  25.  
  26.     private static HttpClient getHttpClient() {
  27.         if (mHttpClient == null) {
  28.             mHttpClient = new DefaultHttpClient();
  29.             final HttpParams params = mHttpClient.getParams();
  30.             HttpConnectionParams.setConnectionTimeout(params, HTTP_TIMEOUT);
  31.             HttpConnectionParams.setSoTimeout(params, HTTP_TIMEOUT);
  32.             ConnManagerParams.setTimeout(params, HTTP_TIMEOUT);
  33.         }
  34.         return mHttpClient;
  35.     }
  36.  
  37.    
  38.     /**
  39.      * Method untuk Mengirimkan data ke server
  40.      */
  41.     public static String executeHttpPost(String url, ArrayList<NameValuePair> postParameters) throws Exception {
  42.         BufferedReader in = null;
  43.         try {
  44.             HttpClient client = getHttpClient();
  45.             HttpPost request = new HttpPost(url);
  46.             UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
  47.             request.setEntity(formEntity);
  48.             HttpResponse response = client.execute(request);
  49.             in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
  50.  
  51.             StringBuffer sb = new StringBuffer("");
  52.             String line = "";
  53.             String NL = System.getProperty("line.separator");
  54.             while ((line = in.readLine()) != null) {
  55.                 sb.append(line + NL);
  56.             }
  57.             in.close();
  58.  
  59.             String result = sb.toString();
  60.             return result;
  61.         } finally {
  62.             if (in != null) {
  63.                 try {
  64.                     in.close();
  65.                 } catch (IOException e) {
  66.                     e.printStackTrace();
  67.                 }
  68.             }
  69.         }
  70.     }
  71.  
  72.    
  73.     /**
  74.      * Method untuk Mengambil data dari server
  75.      */
  76.     public static String executeHttpGet(String url) throws Exception {
  77.         BufferedReader in = null;
  78.         try {
  79.             HttpClient client = getHttpClient();
  80.             HttpGet request = new HttpGet();
  81.             request.setURI(new URI(url));
  82.             HttpResponse response = client.execute(request);
  83.             in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
  84.  
  85.             StringBuffer sb = new StringBuffer("");
  86.             String line = "";
  87.             String NL = System.getProperty("line.separator");
  88.             while ((line = in.readLine()) != null) {
  89.                 sb.append(line + NL);
  90.             }
  91.             in.close();
  92.  
  93.             String result = sb.toString();
  94.             return result;
  95.         } finally {
  96.             if (in != null) {
  97.                 try {
  98.                     in.close();
  99.                 } catch (IOException e) {
  100.                     e.printStackTrace();
  101.                 }
  102.             }
  103.         }
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement