7 package com.google.appinventor.components.runtime.util;
9 import android.util.Log;
11 import org.apache.http.NameValuePair;
12 import org.apache.http.client.ClientProtocolException;
13 import org.apache.http.client.HttpClient;
14 import org.apache.http.client.ResponseHandler;
15 import org.apache.http.client.entity.UrlEncodedFormEntity;
16 import org.apache.http.client.methods.HttpPost;
17 import org.apache.http.conn.params.ConnManagerParams;
18 import org.apache.http.conn.scheme.PlainSocketFactory;
19 import org.apache.http.conn.scheme.Scheme;
20 import org.apache.http.conn.scheme.SchemeRegistry;
21 import org.apache.http.conn.ssl.SSLSocketFactory;
22 import org.apache.http.impl.client.BasicResponseHandler;
23 import org.apache.http.impl.client.DefaultHttpClient;
24 import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
25 import org.apache.http.params.BasicHttpParams;
26 import org.apache.http.params.HttpConnectionParams;
27 import org.apache.http.protocol.HTTP;
28 import org.json.JSONArray;
29 import org.json.JSONException;
30 import org.json.JSONObject;
32 import java.io.IOException;
33 import java.io.UnsupportedEncodingException;
34 import java.util.ArrayList;
35 import java.util.List;
48 private static final String LOG_TAG =
"WebServiceUtil";
49 private static HttpClient httpClient =
null;
50 private static Object httpClientSynchronizer =
new Object();
63 synchronized(httpClientSynchronizer) {
64 if (httpClient ==
null) {
65 SchemeRegistry schemeRegistry =
new SchemeRegistry();
66 schemeRegistry.register(
new Scheme(
"http", PlainSocketFactory.getSocketFactory(), 80));
67 schemeRegistry.register(
new Scheme(
"https", SSLSocketFactory.getSocketFactory(), 443));
68 BasicHttpParams params =
new BasicHttpParams();
69 HttpConnectionParams.setConnectionTimeout(params, 20 * 1000);
70 HttpConnectionParams.setSoTimeout(params, 20 * 1000);
71 ConnManagerParams.setMaxTotalConnections(params, 20);
72 ThreadSafeClientConnManager manager =
new ThreadSafeClientConnManager(params,
74 WebServiceUtil.httpClient =
new DefaultHttpClient(manager, params);
94 public void onSuccess(String httpResponseString) {
96 callback.
onSuccess(
new JSONArray(httpResponseString));
97 }
catch (JSONException e) {
101 public void onFailure(String failureMessage) {
105 postCommand(serviceURL, commandName, params, thisCallback);
122 public void onSuccess(String httpResponseString) {
124 callback.
onSuccess(
new JSONObject(httpResponseString));
125 }
catch (JSONException e) {
129 public void onFailure(String failureMessage) {
133 postCommand(serviceURL, commandName, params, thisCallback);
147 public void postCommand(
final String serviceURL,
final String commandName,
149 Log.d(LOG_TAG,
"Posting " + commandName +
" to " + serviceURL +
" with arguments " + params);
151 if (serviceURL ==
null || serviceURL.equals(
"")) {
152 callback.
onFailure(
"No service url to post command to.");
154 final HttpPost httpPost =
new HttpPost(serviceURL +
"/" + commandName);
156 if (params ==
null) {
157 params =
new ArrayList<NameValuePair>();
160 String httpResponseString;
161 ResponseHandler<String> responseHandler =
new BasicResponseHandler();
162 httpPost.setEntity(
new UrlEncodedFormEntity(params, HTTP.UTF_8));
163 httpPost.setHeader(
"Accept",
"application/json");
164 httpResponseString = httpClient.execute(httpPost, responseHandler);
166 }
catch (UnsupportedEncodingException e) {
168 callback.
onFailure(
"Failed to encode params for web service call.");
169 }
catch (ClientProtocolException e) {
171 callback.
onFailure(
"Communication with the web service encountered a protocol exception.");
172 }
catch (IOException e) {
174 callback.
onFailure(
"Communication with the web service timed out.");