5.0.android.md.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <meta http-equiv="Content-Style-Type" content="text/css" />
  6. <meta name="generator" content="pandoc" />
  7. <title></title>
  8. <style type="text/css">code{white-space: pre;}</style>
  9. <style type="text/css">
  10. div.sourceCode { overflow-x: auto; }
  11. table.sourceCode, tr.sourceCode, td.lineNumbers, td.sourceCode {
  12. margin: 0; padding: 0; vertical-align: baseline; border: none; }
  13. table.sourceCode { width: 100%; line-height: 100%; }
  14. td.lineNumbers { text-align: right; padding-right: 4px; padding-left: 4px; color: #aaaaaa; border-right: 1px solid #aaaaaa; }
  15. td.sourceCode { padding-left: 5px; }
  16. code > span.kw { color: #007020; font-weight: bold; } /* Keyword */
  17. code > span.dt { color: #902000; } /* DataType */
  18. code > span.dv { color: #40a070; } /* DecVal */
  19. code > span.bn { color: #40a070; } /* BaseN */
  20. code > span.fl { color: #40a070; } /* Float */
  21. code > span.ch { color: #4070a0; } /* Char */
  22. code > span.st { color: #4070a0; } /* String */
  23. code > span.co { color: #60a0b0; font-style: italic; } /* Comment */
  24. code > span.ot { color: #007020; } /* Other */
  25. code > span.al { color: #ff0000; font-weight: bold; } /* Alert */
  26. code > span.fu { color: #06287e; } /* Function */
  27. code > span.er { color: #ff0000; font-weight: bold; } /* Error */
  28. code > span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
  29. code > span.cn { color: #880000; } /* Constant */
  30. code > span.sc { color: #4070a0; } /* SpecialChar */
  31. code > span.vs { color: #4070a0; } /* VerbatimString */
  32. code > span.ss { color: #bb6688; } /* SpecialString */
  33. code > span.im { } /* Import */
  34. code > span.va { color: #19177c; } /* Variable */
  35. code > span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
  36. code > span.op { color: #666666; } /* Operator */
  37. code > span.bu { } /* BuiltIn */
  38. code > span.ex { } /* Extension */
  39. code > span.pp { color: #bc7a00; } /* Preprocessor */
  40. code > span.at { color: #7d9029; } /* Attribute */
  41. code > span.do { color: #ba2121; font-style: italic; } /* Documentation */
  42. code > span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
  43. code > span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
  44. code > span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
  45. </style>
  46. <link rel="stylesheet" href="css/vendor.css" type="text/css" />
  47. </head>
  48. <body>
  49. <html xmlns="http://www.w3.org/1999/xhtml" xmlns:wb="http://open.weibo.com/wb" >
  50. <head>
  51. <title>一步步搭建物联网系统(教你设计物联网系统)</title>
  52. <meta name="keywords" content="设计物联网系统,物联网系统设计">
  53. <meta name="description" content="一份关于如何设计物联网系统的文档">
  54. <link rel="stylesheet" href="../css/vendor.css" />
  55. </head>
  56. <body class="container inner wrap">
  57. <h1>一步步搭建物联网系统(教你设计物联网系统)</h1>
  58. </body>
  59. </html>
  60. <div id="TOC">
  61. <ul>
  62. <li><a href="#android简单示例"><span class="toc-section-number">1</span> Android简单示例</a><ul>
  63. <li><a href="#调用web-services-get"><span class="toc-section-number">1.1</span> 调用Web Services GET</a><ul>
  64. <li><a href="#创建restclient"><span class="toc-section-number">1.1.1</span> 创建RESTClient</a></li>
  65. </ul></li>
  66. <li><a href="#使用rest-client获取结果"><span class="toc-section-number">1.2</span> 使用REST Client获取结果</a></li>
  67. </ul></li>
  68. </ul>
  69. </div>
  70. <h1 id="android简单示例"><span class="header-section-number">1</span> Android简单示例</h1>
  71. <p>由于在某些嵌入式系统中使用的是Android系统,这里给出一个简单的Android App的示例,具体代码可以从clone自<a href="https://github.com/phodal/iot-android" class="uri">https://github.com/phodal/iot-android</a></p>
  72. <p>代码说明,经过测试的版本有</p>
  73. <ul>
  74. <li>Android 2.3</li>
  75. <li>Android 4.0.4</li>
  76. </ul>
  77. <p>机型有</p>
  78. <ul>
  79. <li>HTC G1 (android 2.3)</li>
  80. <li>Motor xt300 (android 2.3)</li>
  81. <li>Sony ST25I (android 4.0.4)</li>
  82. <li>MI2</li>
  83. </ul>
  84. <p>应该可以在大部分的手机上工作。</p>
  85. <h2 id="调用web-services-get"><span class="header-section-number">1.1</span> 调用Web Services GET</h2>
  86. <p>这里我们参考一篇文章来调用Web Services——<a href="http://lukencode.com/2010/04/27/calling-web-services-in-android-using-httpclient/">Calling Web Services in Android using HttpClient</a></p>
  87. <h3 id="创建restclient"><span class="header-section-number">1.1.1</span> 创建RESTClient</h3>
  88. <p>在这里我们首先会定义四个REST方法GET、POST、PUT、DELETE</p>
  89. <div class="sourceCode"><pre class="sourceCode java"><code class="sourceCode java"><span class="kw">public</span> <span class="dt">void</span> <span class="fu">Execute</span>(RequestMethod method) <span class="kw">throws</span> Exception {
  90. <span class="kw">switch</span> (method) {
  91. <span class="kw">case</span> GET: {
  92. <span class="co">// add parameters</span>
  93. String combinedParams = <span class="st">&quot;&quot;</span>;
  94. <span class="kw">if</span> (!params.<span class="fu">isEmpty</span>()) {
  95. combinedParams += <span class="st">&quot;?&quot;</span>;
  96. <span class="kw">for</span> (NameValuePair p : params) {
  97. String paramString = p.<span class="fu">getName</span>() + <span class="st">&quot;=&quot;</span>
  98. + URLEncoder.<span class="fu">encode</span>(p.<span class="fu">getValue</span>(), HTTP.<span class="fu">UTF_8</span>);
  99. <span class="kw">if</span> (combinedParams.<span class="fu">length</span>() &gt; <span class="dv">1</span>) {
  100. combinedParams += <span class="st">&quot;&amp;&quot;</span> + paramString;
  101. } <span class="kw">else</span> {
  102. combinedParams += paramString;
  103. }
  104. }
  105. }
  106. HttpGet request = <span class="kw">new</span> <span class="fu">HttpGet</span>(url + combinedParams);
  107. request.<span class="fu">addHeader</span>(<span class="st">&quot;Accept-Encoding&quot;</span>, <span class="st">&quot;gzip&quot;</span>);
  108. <span class="co">// add headers</span>
  109. <span class="kw">for</span> (NameValuePair h : headers) {
  110. request.<span class="fu">addHeader</span>(h.<span class="fu">getName</span>(), h.<span class="fu">getValue</span>());
  111. }
  112. <span class="fu">executeRequest</span>(request, url);
  113. <span class="kw">break</span>;
  114. }
  115. <span class="kw">case</span> POST: {
  116. HttpPost request = <span class="kw">new</span> <span class="fu">HttpPost</span>(url);
  117. request.<span class="fu">addHeader</span>(<span class="st">&quot;Accept-Encoding&quot;</span>, <span class="st">&quot;gzip&quot;</span>);
  118. <span class="co">// add headers</span>
  119. <span class="kw">for</span> (NameValuePair h : headers) {
  120. request.<span class="fu">addHeader</span>(h.<span class="fu">getName</span>(), h.<span class="fu">getValue</span>());
  121. }
  122. <span class="kw">if</span> (!data.<span class="fu">equals</span>(<span class="st">&quot;&quot;</span>)) {
  123. request.<span class="fu">setEntity</span>(<span class="kw">new</span> <span class="fu">StringEntity</span>(data, HTTP.<span class="fu">UTF_8</span>));
  124. }
  125. <span class="kw">if</span> (!params.<span class="fu">isEmpty</span>()) {
  126. request.<span class="fu">setEntity</span>(<span class="kw">new</span> <span class="fu">UrlEncodedFormEntity</span>(params, HTTP.<span class="fu">UTF_8</span>));
  127. }
  128. <span class="fu">executeRequest</span>(request, url);
  129. <span class="kw">break</span>;
  130. }
  131. <span class="kw">case</span> PUT: {
  132. HttpPut request = <span class="kw">new</span> <span class="fu">HttpPut</span>(url);
  133. request.<span class="fu">addHeader</span>(<span class="st">&quot;Accept-Encoding&quot;</span>, <span class="st">&quot;gzip&quot;</span>);
  134. <span class="co">// add headers</span>
  135. <span class="kw">for</span> (NameValuePair h : headers) {
  136. request.<span class="fu">addHeader</span>(h.<span class="fu">getName</span>(), h.<span class="fu">getValue</span>());
  137. }
  138. <span class="kw">if</span> (!data.<span class="fu">equals</span>(<span class="st">&quot;&quot;</span>)) {
  139. request.<span class="fu">setEntity</span>(<span class="kw">new</span> <span class="fu">StringEntity</span>(data, HTTP.<span class="fu">UTF_8</span>));
  140. }
  141. <span class="kw">if</span> (!params.<span class="fu">isEmpty</span>()) {
  142. request.<span class="fu">setEntity</span>(<span class="kw">new</span> <span class="fu">UrlEncodedFormEntity</span>(params, HTTP.<span class="fu">UTF_8</span>));
  143. }
  144. <span class="fu">executeRequest</span>(request, url);
  145. <span class="kw">break</span>;
  146. }
  147. <span class="kw">case</span> DELETE: {
  148. HttpDelete request = <span class="kw">new</span> <span class="fu">HttpDelete</span>(url);
  149. request.<span class="fu">addHeader</span>(<span class="st">&quot;Accept-Encoding&quot;</span>, <span class="st">&quot;gzip&quot;</span>);
  150. <span class="co">// add headers</span>
  151. <span class="kw">for</span> (NameValuePair h : headers) {
  152. request.<span class="fu">addHeader</span>(h.<span class="fu">getName</span>(), h.<span class="fu">getValue</span>());
  153. }
  154. <span class="fu">executeRequest</span>(request, url);
  155. <span class="kw">break</span>;
  156. }
  157. }
  158. }</code></pre></div>
  159. <p>这四个方法最后都执行executeRequest来获取响应结果。</p>
  160. <div class="sourceCode"><pre class="sourceCode java"><code class="sourceCode java"><span class="kw">protected</span> <span class="dt">void</span> <span class="fu">executeRequest</span>(HttpUriRequest request, String url) {
  161. HttpParams httpParameters = <span class="kw">new</span> <span class="fu">BasicHttpParams</span>();
  162. HttpConnectionParams.<span class="fu">setConnectionTimeout</span>(httpParameters,
  163. timeoutConnection);
  164. HttpConnectionParams.<span class="fu">setSoTimeout</span>(httpParameters, timeoutSocket);
  165. HttpProtocolParams.<span class="fu">setUseExpectContinue</span>(httpParameters, <span class="kw">false</span>);
  166. request.<span class="fu">setParams</span>(httpParameters);
  167. <span class="fu">setOauth</span>(request);
  168. DefaultHttpClient client = <span class="kw">new</span> <span class="fu">DefaultHttpClient</span>();
  169. HttpResponse httpResponse;
  170. <span class="kw">try</span> {
  171. httpResponse = client.<span class="fu">execute</span>(request);
  172. responseCode = httpResponse.<span class="fu">getStatusLine</span>().<span class="fu">getStatusCode</span>();
  173. message = httpResponse.<span class="fu">getStatusLine</span>().<span class="fu">getReasonPhrase</span>();
  174. HttpEntity entity = httpResponse.<span class="fu">getEntity</span>();
  175. <span class="kw">if</span> (entity != <span class="kw">null</span>) {
  176. InputStream instream = httpResponse.<span class="fu">getEntity</span>().<span class="fu">getContent</span>();
  177. Header contentEncoding = httpResponse
  178. .<span class="fu">getFirstHeader</span>(<span class="st">&quot;Content-Encoding&quot;</span>);
  179. <span class="kw">if</span> (contentEncoding != <span class="kw">null</span>
  180. &amp;&amp; contentEncoding.<span class="fu">getValue</span>().<span class="fu">equalsIgnoreCase</span>(<span class="st">&quot;gzip&quot;</span>)) {
  181. instream = <span class="kw">new</span> GZIPInputStream(instream);
  182. }
  183. <span class="co">// instream = entity.getContent();</span>
  184. response = <span class="fu">convertStreamToString</span>(instream);
  185. <span class="co">// Closing the input stream will trigger connection release</span>
  186. instream.<span class="fu">close</span>();
  187. }
  188. } <span class="kw">catch</span> (ClientProtocolException e) {
  189. client.<span class="fu">getConnectionManager</span>().<span class="fu">shutdown</span>();
  190. e.<span class="fu">printStackTrace</span>();
  191. } <span class="kw">catch</span> (IOException e) {
  192. client.<span class="fu">getConnectionManager</span>().<span class="fu">shutdown</span>();
  193. e.<span class="fu">printStackTrace</span>();
  194. }
  195. }</code></pre></div>
  196. <p>接着,我们便可以执行getResponse()函数来获取结果。</p>
  197. <h2 id="使用rest-client获取结果"><span class="header-section-number">1.2</span> 使用REST Client获取结果</h2>
  198. <p>使用RESTClient时,便可以用下面的示例</p>
  199. <div class="sourceCode"><pre class="sourceCode java"><code class="sourceCode java">RestClient client = <span class="kw">new</span> <span class="fu">RestClient</span>(tUrl);
  200. <span class="kw">try</span> {
  201. client.<span class="fu">Execute</span>(RequestMethod.<span class="fu">GET</span>);
  202. <span class="kw">if</span> (client.<span class="fu">getResponseCode</span>() != <span class="dv">200</span>) {
  203. <span class="co">//do something</span>
  204. }
  205. <span class="co">//JSONArray jArray = new JSONArray(client.getResponse());</span>
  206. } <span class="kw">catch</span> (Exception e) {
  207. <span class="co">//do something</span>
  208. }</code></pre></div>
  209. <p>而这时,我们只需要对相应的数据进行处理就可以了,如</p>
  210. <div class="sourceCode"><pre class="sourceCode java"><code class="sourceCode java">JSONArray jArray = <span class="kw">new</span> <span class="fu">JSONArray</span>(client.<span class="fu">getResponse</span>());
  211. JSONObject jObj=jArray.<span class="fu">getJSONObject</span>(<span class="dv">0</span>);
  212. vshow.<span class="fu">setText</span>(jObj.<span class="fu">toString</span>());
  213. <span class="fu">outputJSON</span>(jObj);</code></pre></div>
  214. <p>将他转换为String,接着在Android端上显示最后的结果。</p>
  215. </body>
  216. </html>