7 package com.google.appinventor.components.runtime;
27 import android.os.Handler;
29 import org.apache.http.NameValuePair;
30 import org.apache.http.message.BasicNameValuePair;
31 import org.json.JSONArray;
32 import org.json.JSONException;
63 @DesignerComponent(version = YaVersion.TINYWEBDB_COMPONENT_VERSION,
64 description =
"Non-visible component that communicates with a Web service to store and " +
65 "retrieve information.",
66 category = ComponentCategory.STORAGE,
68 iconName =
"images/tinyWebDB.png")
70 @UsesPermissions(permissionNames =
"android.permission.INTERNET")
73 private static final String LOG_TAG =
"TinyWebDB";
74 private static final String STOREAVALUE_COMMAND =
"storeavalue";
75 private static final String TAG_PARAMETER =
"tag";
76 private static final String VALUE_PARAMETER =
"value";
77 private static final String GETVALUE_COMMAND =
"getvalue";
79 private String serviceURL;
80 private Handler androidUIHandler;
88 super(container.
$form());
94 androidUIHandler =
new Handler();
97 serviceURL =
"http://tinywebdb.appinventor.mit.edu/";
118 description =
"The URL of the web service database.")
119 public String ServiceURL() {
128 defaultValue =
"http://tinywebdb.appinventor.mit.edu")
153 @
SimpleFunction(description =
"Asks the Web service to store the given value under the given tag")
156 public
void StoreValue(final String tag, final Object valueToStore) {
157 final Runnable call =
new Runnable() {
158 public void run() { postStoreValue(tag, valueToStore); }};
178 private void postStoreValue(String tag, Object valueToStore) {
188 public void onSuccess(String response) {
192 androidUIHandler.post(
new Runnable() {
202 public void onFailure(
final String message) {
205 androidUIHandler.post(
new Runnable() {
207 WebServiceError(message);
213 WebServiceUtil.getInstance().postCommand(serviceURL,
215 Lists.<NameValuePair>newArrayList(
216 new BasicNameValuePair(TAG_PARAMETER, tag),
217 new BasicNameValuePair(VALUE_PARAMETER,
218 JsonUtil.getJsonRepresentation(valueToStore))),
220 }
catch (JSONException e) {
221 throw new YailRuntimeError(
"Value failed to convert to JSON.",
"JSON Creation Error.");
233 @SimpleEvent(description =
"Event indicating that a StoreValue server request has succeeded.")
234 public
void ValueStored() {
263 @
SimpleFunction(description =
"Sends a request to the Web service to get the value stored under "
264 +
"the given tag. The Web service must decide what to return if there is no value stored "
265 +
"under the tag. This component accepts whatever is returned.")
266 public
void GetValue(final String tag) {
267 final Runnable call =
new Runnable() {
public void run() { postGetValue(tag); }};
271 private void postGetValue(
final String tag) {
274 public void onSuccess(JSONArray result) {
275 if (result ==
null) {
278 androidUIHandler.post(
new Runnable() {
280 WebServiceError(
"The Web server did not respond to the get value request " +
281 "for the tag " + tag +
".");
289 final String tagFromWebDB = result.getString(1);
290 String value = result.getString(2);
292 final Object valueFromWebDB = (value.length() == 0) ?
"" :
293 JsonUtil.getObjectFromJson(value,
true);
294 androidUIHandler.post(
new Runnable() {
298 GotValue(tagFromWebDB, valueFromWebDB);
301 }
catch (JSONException e) {
306 androidUIHandler.post(
new Runnable() {
308 WebServiceError(
"The Web server returned a garbled value " +
309 "for the tag " + tag +
".");
316 public void onFailure(
final String message) {
320 androidUIHandler.post(
new Runnable() {
322 WebServiceError(message);
328 WebServiceUtil.getInstance().postCommandReturningArray(
331 Lists.<NameValuePair>newArrayList(
new BasicNameValuePair(TAG_PARAMETER, tag)),
342 @SimpleEvent(description =
"Indicates that a GetValue server request has succeeded.")
343 public
void GotValue(String tagFromWebDB, Object valueFromWebDB) {