Android简单示例

由于在某些嵌入式系统中使用的是Android系统,这里给出一个简单的Android App的示例,具体代码可以从clone自https://github.com/phodal/iot-android

代码说明,经过测试的版本有

  • Android 2.3
  • Android 4.0.4

机型有

  • HTC G1 (android 2.3)
  • Motor xt300 (android 2.3)
  • Sony ST25I (android 4.0.4)
  • MI2

应该可以在大部分的手机上工作。

调用Web Services GET

这里我们参考一篇文章来调用Web Services——Calling Web Services in Android using HttpClient

创建RESTClient

在这里我们首先会定义四个REST方法GET、POST、PUT、DELETE

  1. public void Execute(RequestMethod method) throws Exception {
  2. switch (method) {
  3. case GET: {
  4. // add parameters
  5. String combinedParams = "";
  6. if (!params.isEmpty()) {
  7. combinedParams += "?";
  8. for (NameValuePair p : params) {
  9. String paramString = p.getName() + "="
  10. + URLEncoder.encode(p.getValue(), HTTP.UTF_8);
  11. if (combinedParams.length() > 1) {
  12. combinedParams += "&" + paramString;
  13. } else {
  14. combinedParams += paramString;
  15. }
  16. }
  17. }
  18. HttpGet request = new HttpGet(url + combinedParams);
  19. request.addHeader("Accept-Encoding", "gzip");
  20. // add headers
  21. for (NameValuePair h : headers) {
  22. request.addHeader(h.getName(), h.getValue());
  23. }
  24. executeRequest(request, url);
  25. break;
  26. }
  27. case POST: {
  28. HttpPost request = new HttpPost(url);
  29. request.addHeader("Accept-Encoding", "gzip");
  30. // add headers
  31. for (NameValuePair h : headers) {
  32. request.addHeader(h.getName(), h.getValue());
  33. }
  34. if (!data.equals("")) {
  35. request.setEntity(new StringEntity(data, HTTP.UTF_8));
  36. }
  37. if (!params.isEmpty()) {
  38. request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
  39. }
  40. executeRequest(request, url);
  41. break;
  42. }
  43. case PUT: {
  44. HttpPut request = new HttpPut(url);
  45. request.addHeader("Accept-Encoding", "gzip");
  46. // add headers
  47. for (NameValuePair h : headers) {
  48. request.addHeader(h.getName(), h.getValue());
  49. }
  50. if (!data.equals("")) {
  51. request.setEntity(new StringEntity(data, HTTP.UTF_8));
  52. }
  53. if (!params.isEmpty()) {
  54. request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
  55. }
  56. executeRequest(request, url);
  57. break;
  58. }
  59. case DELETE: {
  60. HttpDelete request = new HttpDelete(url);
  61. request.addHeader("Accept-Encoding", "gzip");
  62. // add headers
  63. for (NameValuePair h : headers) {
  64. request.addHeader(h.getName(), h.getValue());
  65. }
  66. executeRequest(request, url);
  67. break;
  68. }
  69. }
  70. }

这四个方法最后都执行executeRequest来获取响应结果。

  1. protected void executeRequest(HttpUriRequest request, String url) {
  2. HttpParams httpParameters = new BasicHttpParams();
  3. HttpConnectionParams.setConnectionTimeout(httpParameters,
  4. timeoutConnection);
  5. HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
  6. HttpProtocolParams.setUseExpectContinue(httpParameters, false);
  7. request.setParams(httpParameters);
  8. setOauth(request);
  9. DefaultHttpClient client = new DefaultHttpClient();
  10. HttpResponse httpResponse;
  11. try {
  12. httpResponse = client.execute(request);
  13. responseCode = httpResponse.getStatusLine().getStatusCode();
  14. message = httpResponse.getStatusLine().getReasonPhrase();
  15. HttpEntity entity = httpResponse.getEntity();
  16. if (entity != null) {
  17. InputStream instream = httpResponse.getEntity().getContent();
  18. Header contentEncoding = httpResponse
  19. .getFirstHeader("Content-Encoding");
  20. if (contentEncoding != null
  21. && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
  22. instream = new GZIPInputStream(instream);
  23. }
  24. // instream = entity.getContent();
  25. response = convertStreamToString(instream);
  26. // Closing the input stream will trigger connection release
  27. instream.close();
  28. }
  29. } catch (ClientProtocolException e) {
  30. client.getConnectionManager().shutdown();
  31. e.printStackTrace();
  32. } catch (IOException e) {
  33. client.getConnectionManager().shutdown();
  34. e.printStackTrace();
  35. }
  36. }

接着,我们便可以执行getResponse()函数来获取结果。

使用REST Client获取结果

使用RESTClient时,便可以用下面的示例

  1. RestClient client = new RestClient(tUrl);
  2. try {
  3. client.Execute(RequestMethod.GET);
  4. if (client.getResponseCode() != 200) {
  5. //do something
  6. }
  7. //JSONArray jArray = new JSONArray(client.getResponse());
  8. } catch (Exception e) {
  9. //do something
  10. }

而这时,我们只需要对相应的数据进行处理就可以了,如

  1. JSONArray jArray = new JSONArray(client.getResponse());
  2. JSONObject jObj=jArray.getJSONObject(0);
  3. vshow.setText(jObj.toString());
  4. outputJSON(jObj);

将他转换为String,接着在Android端上显示最后的结果。