1 package com.example.xubin.irrigation;
3 import android.util.Log;
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;
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;
31 * Created by xubin on 4/26/16.
34 private static final int Driver_port = 8000;
35 private static final String Tag = "http";
37 HttpClient httpclient;
39 void setConnection() {
40 httpclient = createClient();
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);
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);
55 return new DefaultHttpClient(conMgr, params);
58 public void makeRequest(String destIP, List<Object> paramObjects, String methodName) {
59 String url = "http://"+ destIP + ":"+Driver_port+ "/"+methodName;
60 InputStream inputStream = null;
63 HttpPost httpPost = new HttpPost(url);
64 JSONArray params = new JSONArray();
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);
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();
83 // 10. convert inputstream to string
84 if(inputStream != null)
85 result = convertInputStreamToString(inputStream);
87 result = "Did not work!";
89 } catch (Exception ex) {
90 if (ex.getMessage() != null) {
91 Log.v(Tag, ex.getMessage());
96 private static String convertInputStreamToString(InputStream inputStream) throws IOException {
97 BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
100 while((line = bufferedReader.readLine()) != null)