AI2 Component  (Version nb184)
WebServiceUtil.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright 2009-2011 Google, All Rights reserved
3 // Copyright 2011-2012 MIT, All rights reserved
4 // Released under the Apache License, Version 2.0
5 // http://www.apache.org/licenses/LICENSE-2.0
6 
7 package com.google.appinventor.components.runtime.util;
8 
9 import android.util.Log;
10 
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;
31 
32 import java.io.IOException;
33 import java.io.UnsupportedEncodingException;
34 import java.util.ArrayList;
35 import java.util.List;
36 
45 public class WebServiceUtil {
46 
47  private static final WebServiceUtil INSTANCE = new WebServiceUtil();
48  private static final String LOG_TAG = "WebServiceUtil";
49  private static HttpClient httpClient = null;
50  private static Object httpClientSynchronizer = new Object();
51 
52  private WebServiceUtil(){
53  }
54 
59  public static WebServiceUtil getInstance() {
60  // This needs to be here instead of in the constructor because
61  // it uses classes that are in the AndroidSDK and thus would
62  // cause Stub! errors when running the component descriptor.
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,
73  schemeRegistry);
74  WebServiceUtil.httpClient = new DefaultHttpClient(manager, params);
75  }
76  }
77  return INSTANCE;
78  }
79 
91  public void postCommandReturningArray(String serviceURL, String commandName,
92  List<NameValuePair> params, final AsyncCallbackPair<JSONArray> callback) {
94  public void onSuccess(String httpResponseString) {
95  try {
96  callback.onSuccess(new JSONArray(httpResponseString));
97  } catch (JSONException e) {
98  callback.onFailure(e.getMessage());
99  }
100  }
101  public void onFailure(String failureMessage) {
102  callback.onFailure(failureMessage);
103  }
104  };
105  postCommand(serviceURL, commandName, params, thisCallback);
106  }
107 
119  public void postCommandReturningObject(final String serviceURL,final String commandName,
120  List<NameValuePair> params, final AsyncCallbackPair<JSONObject> callback) {
122  public void onSuccess(String httpResponseString) {
123  try {
124  callback.onSuccess(new JSONObject(httpResponseString));
125  } catch (JSONException e) {
126  callback.onFailure(e.getMessage());
127  }
128  }
129  public void onFailure(String failureMessage) {
130  callback.onFailure(failureMessage);
131  }
132  };
133  postCommand(serviceURL, commandName, params, thisCallback);
134  }
135 
147  public void postCommand(final String serviceURL, final String commandName,
148  List<NameValuePair> params, AsyncCallbackPair<String> callback) {
149  Log.d(LOG_TAG, "Posting " + commandName + " to " + serviceURL + " with arguments " + params);
150 
151  if (serviceURL == null || serviceURL.equals("")) {
152  callback.onFailure("No service url to post command to.");
153  }
154  final HttpPost httpPost = new HttpPost(serviceURL + "/" + commandName);
155 
156  if (params == null) {
157  params = new ArrayList<NameValuePair>();
158  }
159  try {
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);
165  callback.onSuccess(httpResponseString);
166  } catch (UnsupportedEncodingException e) {
167  Log.w(LOG_TAG, e);
168  callback.onFailure("Failed to encode params for web service call.");
169  } catch (ClientProtocolException e) {
170  Log.w(LOG_TAG, e);
171  callback.onFailure("Communication with the web service encountered a protocol exception.");
172  } catch (IOException e) {
173  Log.w(LOG_TAG, e);
174  callback.onFailure("Communication with the web service timed out.");
175  }
176  }
177 }
com.google.appinventor.components.runtime.util.AsyncCallbackPair.onFailure
void onFailure(String message)
com.google.appinventor.components.runtime.util.WebServiceUtil.postCommand
void postCommand(final String serviceURL, final String commandName, List< NameValuePair > params, AsyncCallbackPair< String > callback)
Definition: WebServiceUtil.java:147
com.google.appinventor.components.runtime.util.WebServiceUtil.postCommandReturningObject
void postCommandReturningObject(final String serviceURL, final String commandName, List< NameValuePair > params, final AsyncCallbackPair< JSONObject > callback)
Definition: WebServiceUtil.java:119
com.google.appinventor.components.runtime.util.WebServiceUtil
Definition: WebServiceUtil.java:45
com.google.appinventor.components.runtime.util.WebServiceUtil.getInstance
static WebServiceUtil getInstance()
Definition: WebServiceUtil.java:59
com.google.appinventor.components.runtime.util.AsyncCallbackPair.onSuccess
void onSuccess(T result)
com.google.appinventor.components.runtime.util.WebServiceUtil.postCommandReturningArray
void postCommandReturningArray(String serviceURL, String commandName, List< NameValuePair > params, final AsyncCallbackPair< JSONArray > callback)
Definition: WebServiceUtil.java:91
com.google.appinventor.components.runtime.util.AsyncCallbackPair
Definition: AsyncCallbackPair.java:17