123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <meta http-equiv="Content-Style-Type" content="text/css" />
- <meta name="generator" content="pandoc" />
- <title></title>
- <style type="text/css">code{white-space: pre;}</style>
- <style type="text/css">
- div.sourceCode { overflow-x: auto; }
- table.sourceCode, tr.sourceCode, td.lineNumbers, td.sourceCode {
- margin: 0; padding: 0; vertical-align: baseline; border: none; }
- table.sourceCode { width: 100%; line-height: 100%; }
- td.lineNumbers { text-align: right; padding-right: 4px; padding-left: 4px; color: #aaaaaa; border-right: 1px solid #aaaaaa; }
- td.sourceCode { padding-left: 5px; }
- code > span.kw { color: #007020; font-weight: bold; } /* Keyword */
- code > span.dt { color: #902000; } /* DataType */
- code > span.dv { color: #40a070; } /* DecVal */
- code > span.bn { color: #40a070; } /* BaseN */
- code > span.fl { color: #40a070; } /* Float */
- code > span.ch { color: #4070a0; } /* Char */
- code > span.st { color: #4070a0; } /* String */
- code > span.co { color: #60a0b0; font-style: italic; } /* Comment */
- code > span.ot { color: #007020; } /* Other */
- code > span.al { color: #ff0000; font-weight: bold; } /* Alert */
- code > span.fu { color: #06287e; } /* Function */
- code > span.er { color: #ff0000; font-weight: bold; } /* Error */
- code > span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
- code > span.cn { color: #880000; } /* Constant */
- code > span.sc { color: #4070a0; } /* SpecialChar */
- code > span.vs { color: #4070a0; } /* VerbatimString */
- code > span.ss { color: #bb6688; } /* SpecialString */
- code > span.im { } /* Import */
- code > span.va { color: #19177c; } /* Variable */
- code > span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
- code > span.op { color: #666666; } /* Operator */
- code > span.bu { } /* BuiltIn */
- code > span.ex { } /* Extension */
- code > span.pp { color: #bc7a00; } /* Preprocessor */
- code > span.at { color: #7d9029; } /* Attribute */
- code > span.do { color: #ba2121; font-style: italic; } /* Documentation */
- code > span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
- code > span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
- code > span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
- </style>
- <link rel="stylesheet" href="css/vendor.css" type="text/css" />
- </head>
- <body>
- <html xmlns="http://www.w3.org/1999/xhtml" xmlns:wb="http://open.weibo.com/wb" >
- <head>
- <title>一步步搭建物联网系统(教你设计物联网系统)</title>
- <meta name="keywords" content="设计物联网系统,物联网系统设计">
- <meta name="description" content="一份关于如何设计物联网系统的文档">
- <link rel="stylesheet" href="../css/vendor.css" />
- </head>
- <body class="container inner wrap">
- <h1>一步步搭建物联网系统(教你设计物联网系统)</h1>
- </body>
- </html>
- <div id="TOC">
- <ul>
- <li><a href="#android简单示例"><span class="toc-section-number">1</span> Android简单示例</a><ul>
- <li><a href="#调用web-services-get"><span class="toc-section-number">1.1</span> 调用Web Services GET</a><ul>
- <li><a href="#创建restclient"><span class="toc-section-number">1.1.1</span> 创建RESTClient</a></li>
- </ul></li>
- <li><a href="#使用rest-client获取结果"><span class="toc-section-number">1.2</span> 使用REST Client获取结果</a></li>
- </ul></li>
- </ul>
- </div>
- <h1 id="android简单示例"><span class="header-section-number">1</span> Android简单示例</h1>
- <p>由于在某些嵌入式系统中使用的是Android系统,这里给出一个简单的Android App的示例,具体代码可以从clone自<a href="https://github.com/phodal/iot-android" class="uri">https://github.com/phodal/iot-android</a></p>
- <p>代码说明,经过测试的版本有</p>
- <ul>
- <li>Android 2.3</li>
- <li>Android 4.0.4</li>
- </ul>
- <p>机型有</p>
- <ul>
- <li>HTC G1 (android 2.3)</li>
- <li>Motor xt300 (android 2.3)</li>
- <li>Sony ST25I (android 4.0.4)</li>
- <li>MI2</li>
- </ul>
- <p>应该可以在大部分的手机上工作。</p>
- <h2 id="调用web-services-get"><span class="header-section-number">1.1</span> 调用Web Services GET</h2>
- <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>
- <h3 id="创建restclient"><span class="header-section-number">1.1.1</span> 创建RESTClient</h3>
- <p>在这里我们首先会定义四个REST方法GET、POST、PUT、DELETE</p>
- <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 {
- <span class="kw">switch</span> (method) {
- <span class="kw">case</span> GET: {
- <span class="co">// add parameters</span>
- String combinedParams = <span class="st">""</span>;
- <span class="kw">if</span> (!params.<span class="fu">isEmpty</span>()) {
- combinedParams += <span class="st">"?"</span>;
- <span class="kw">for</span> (NameValuePair p : params) {
- String paramString = p.<span class="fu">getName</span>() + <span class="st">"="</span>
- + URLEncoder.<span class="fu">encode</span>(p.<span class="fu">getValue</span>(), HTTP.<span class="fu">UTF_8</span>);
- <span class="kw">if</span> (combinedParams.<span class="fu">length</span>() > <span class="dv">1</span>) {
- combinedParams += <span class="st">"&"</span> + paramString;
- } <span class="kw">else</span> {
- combinedParams += paramString;
- }
- }
- }
- HttpGet request = <span class="kw">new</span> <span class="fu">HttpGet</span>(url + combinedParams);
- request.<span class="fu">addHeader</span>(<span class="st">"Accept-Encoding"</span>, <span class="st">"gzip"</span>);
- <span class="co">// add headers</span>
- <span class="kw">for</span> (NameValuePair h : headers) {
- request.<span class="fu">addHeader</span>(h.<span class="fu">getName</span>(), h.<span class="fu">getValue</span>());
- }
- <span class="fu">executeRequest</span>(request, url);
- <span class="kw">break</span>;
- }
- <span class="kw">case</span> POST: {
- HttpPost request = <span class="kw">new</span> <span class="fu">HttpPost</span>(url);
- request.<span class="fu">addHeader</span>(<span class="st">"Accept-Encoding"</span>, <span class="st">"gzip"</span>);
- <span class="co">// add headers</span>
- <span class="kw">for</span> (NameValuePair h : headers) {
- request.<span class="fu">addHeader</span>(h.<span class="fu">getName</span>(), h.<span class="fu">getValue</span>());
- }
- <span class="kw">if</span> (!data.<span class="fu">equals</span>(<span class="st">""</span>)) {
- request.<span class="fu">setEntity</span>(<span class="kw">new</span> <span class="fu">StringEntity</span>(data, HTTP.<span class="fu">UTF_8</span>));
- }
- <span class="kw">if</span> (!params.<span class="fu">isEmpty</span>()) {
- request.<span class="fu">setEntity</span>(<span class="kw">new</span> <span class="fu">UrlEncodedFormEntity</span>(params, HTTP.<span class="fu">UTF_8</span>));
- }
- <span class="fu">executeRequest</span>(request, url);
- <span class="kw">break</span>;
- }
- <span class="kw">case</span> PUT: {
- HttpPut request = <span class="kw">new</span> <span class="fu">HttpPut</span>(url);
- request.<span class="fu">addHeader</span>(<span class="st">"Accept-Encoding"</span>, <span class="st">"gzip"</span>);
- <span class="co">// add headers</span>
- <span class="kw">for</span> (NameValuePair h : headers) {
- request.<span class="fu">addHeader</span>(h.<span class="fu">getName</span>(), h.<span class="fu">getValue</span>());
- }
- <span class="kw">if</span> (!data.<span class="fu">equals</span>(<span class="st">""</span>)) {
- request.<span class="fu">setEntity</span>(<span class="kw">new</span> <span class="fu">StringEntity</span>(data, HTTP.<span class="fu">UTF_8</span>));
- }
- <span class="kw">if</span> (!params.<span class="fu">isEmpty</span>()) {
- request.<span class="fu">setEntity</span>(<span class="kw">new</span> <span class="fu">UrlEncodedFormEntity</span>(params, HTTP.<span class="fu">UTF_8</span>));
- }
- <span class="fu">executeRequest</span>(request, url);
- <span class="kw">break</span>;
- }
- <span class="kw">case</span> DELETE: {
- HttpDelete request = <span class="kw">new</span> <span class="fu">HttpDelete</span>(url);
- request.<span class="fu">addHeader</span>(<span class="st">"Accept-Encoding"</span>, <span class="st">"gzip"</span>);
- <span class="co">// add headers</span>
- <span class="kw">for</span> (NameValuePair h : headers) {
- request.<span class="fu">addHeader</span>(h.<span class="fu">getName</span>(), h.<span class="fu">getValue</span>());
- }
- <span class="fu">executeRequest</span>(request, url);
- <span class="kw">break</span>;
- }
- }
- }</code></pre></div>
- <p>这四个方法最后都执行executeRequest来获取响应结果。</p>
- <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) {
- HttpParams httpParameters = <span class="kw">new</span> <span class="fu">BasicHttpParams</span>();
- HttpConnectionParams.<span class="fu">setConnectionTimeout</span>(httpParameters,
- timeoutConnection);
- HttpConnectionParams.<span class="fu">setSoTimeout</span>(httpParameters, timeoutSocket);
-
- HttpProtocolParams.<span class="fu">setUseExpectContinue</span>(httpParameters, <span class="kw">false</span>);
- request.<span class="fu">setParams</span>(httpParameters);
-
- <span class="fu">setOauth</span>(request);
-
- DefaultHttpClient client = <span class="kw">new</span> <span class="fu">DefaultHttpClient</span>();
- HttpResponse httpResponse;
- <span class="kw">try</span> {
- httpResponse = client.<span class="fu">execute</span>(request);
- responseCode = httpResponse.<span class="fu">getStatusLine</span>().<span class="fu">getStatusCode</span>();
- message = httpResponse.<span class="fu">getStatusLine</span>().<span class="fu">getReasonPhrase</span>();
- HttpEntity entity = httpResponse.<span class="fu">getEntity</span>();
- <span class="kw">if</span> (entity != <span class="kw">null</span>) {
- InputStream instream = httpResponse.<span class="fu">getEntity</span>().<span class="fu">getContent</span>();
- Header contentEncoding = httpResponse
- .<span class="fu">getFirstHeader</span>(<span class="st">"Content-Encoding"</span>);
- <span class="kw">if</span> (contentEncoding != <span class="kw">null</span>
- && contentEncoding.<span class="fu">getValue</span>().<span class="fu">equalsIgnoreCase</span>(<span class="st">"gzip"</span>)) {
- instream = <span class="kw">new</span> GZIPInputStream(instream);
- }
- <span class="co">// instream = entity.getContent();</span>
- response = <span class="fu">convertStreamToString</span>(instream);
- <span class="co">// Closing the input stream will trigger connection release</span>
- instream.<span class="fu">close</span>();
- }
- } <span class="kw">catch</span> (ClientProtocolException e) {
- client.<span class="fu">getConnectionManager</span>().<span class="fu">shutdown</span>();
- e.<span class="fu">printStackTrace</span>();
- } <span class="kw">catch</span> (IOException e) {
- client.<span class="fu">getConnectionManager</span>().<span class="fu">shutdown</span>();
- e.<span class="fu">printStackTrace</span>();
- }
- }</code></pre></div>
- <p>接着,我们便可以执行getResponse()函数来获取结果。</p>
- <h2 id="使用rest-client获取结果"><span class="header-section-number">1.2</span> 使用REST Client获取结果</h2>
- <p>使用RESTClient时,便可以用下面的示例</p>
- <div class="sourceCode"><pre class="sourceCode java"><code class="sourceCode java">RestClient client = <span class="kw">new</span> <span class="fu">RestClient</span>(tUrl);
- <span class="kw">try</span> {
- client.<span class="fu">Execute</span>(RequestMethod.<span class="fu">GET</span>);
- <span class="kw">if</span> (client.<span class="fu">getResponseCode</span>() != <span class="dv">200</span>) {
- <span class="co">//do something</span>
- }
- <span class="co">//JSONArray jArray = new JSONArray(client.getResponse());</span>
- } <span class="kw">catch</span> (Exception e) {
- <span class="co">//do something</span>
- }</code></pre></div>
- <p>而这时,我们只需要对相应的数据进行处理就可以了,如</p>
- <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>());
- JSONObject jObj=jArray.<span class="fu">getJSONObject</span>(<span class="dv">0</span>);
- vshow.<span class="fu">setText</span>(jObj.<span class="fu">toString</span>());
- <span class="fu">outputJSON</span>(jObj);</code></pre></div>
- <p>将他转换为String,接着在Android端上显示最后的结果。</p>
- </body>
- </html>
|