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