Adding config file for sharing.
[iot2.git] / benchmarks / other / PhoneInterface / Irrigation / app / src / main / java / com / example / xubin / irrigation / Helper.java
1 package com.example.xubin.irrigation;
2
3 import android.util.Log;
4
5 import org.apache.http.HttpResponse;
6 import org.apache.http.HttpVersion;
7 import org.apache.http.client.HttpClient;
8 import org.apache.http.client.methods.HttpPost;
9 import org.apache.http.conn.ClientConnectionManager;
10 import org.apache.http.conn.scheme.PlainSocketFactory;
11 import org.apache.http.conn.scheme.Scheme;
12 import org.apache.http.conn.scheme.SchemeRegistry;
13 import org.apache.http.entity.StringEntity;
14 import org.apache.http.impl.client.DefaultHttpClient;
15 import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
16 import org.apache.http.params.BasicHttpParams;
17 import org.apache.http.params.CoreConnectionPNames;
18 import org.apache.http.params.CoreProtocolPNames;
19 import org.apache.http.params.HttpParams;
20 import org.apache.http.protocol.HTTP;
21 import org.json.JSONArray;
22 import org.json.JSONObject;
23
24 import java.io.BufferedReader;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.InputStreamReader;
28 import java.util.List;
29
30 /**
31  * Created by xubin on 4/26/16.
32  */
33 public class Helper {
34     private static final int Driver_port = 8000;
35     private static final String Tag = "http";
36
37     HttpClient httpclient;
38     //Set up
39     void setConnection() {
40         httpclient = createClient();
41     }
42     HttpClient createClient() {
43         HttpParams params = new BasicHttpParams();
44         params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
45         params.setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, HTTP.DEFAULT_CONTENT_CHARSET);
46         params.setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, true);
47         params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30 * 1000);
48         params.setParameter(CoreConnectionPNames.SO_TIMEOUT, 30 * 1000);
49
50         SchemeRegistry schReg = new SchemeRegistry();
51         schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), Driver_port));
52         //schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
53         ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg);
54
55         return new DefaultHttpClient(conMgr, params);
56     }
57     //Make http request
58     public void  makeRequest(String destIP, List<Object> paramObjects, String methodName) {
59         String url = "http://"+ destIP + ":"+Driver_port+ "/"+methodName;
60         InputStream inputStream = null;
61         String result = "";
62         try {
63             HttpPost httpPost = new HttpPost(url);
64             JSONArray params = new JSONArray();
65
66
67             JSONObject parent = new JSONObject();
68             for (int i = 0; i < paramObjects.size(); i++) {
69                 JSONObject content = new JSONObject();
70                 content.put("type", paramObjects.get(i).getClass().getName());
71                 content.put("value", paramObjects.get(i));
72                 params.put(i, content);
73             }
74             parent.put("params", params);
75             StringEntity se = new StringEntity(parent.toString());
76             httpPost.setEntity(se);
77             httpPost.setHeader("Accept", "application/json");
78             httpPost.setHeader("Content-type", "application/json");
79             HttpResponse httpResponse = httpclient.execute(httpPost);
80             // 9. receive response as inputStream
81             inputStream = httpResponse.getEntity().getContent();
82
83             // 10. convert inputstream to string
84             if(inputStream != null)
85                 result = convertInputStreamToString(inputStream);
86             else
87                 result = "Did not work!";
88             Log.v(Tag, result);
89         } catch (Exception ex) {
90             if (ex.getMessage() != null) {
91                 Log.v(Tag, ex.getMessage());
92             }
93             ex.printStackTrace();
94         }
95     }
96     private static String convertInputStreamToString(InputStream inputStream) throws IOException {
97         BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
98         String line = "";
99         String result = "";
100         while((line = bufferedReader.readLine()) != null)
101             result += line;
102
103         inputStream.close();
104         return result;
105
106     }
107 }