<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # Andriod簡單示例 由于在某些嵌入式系統中使用的是Android系統,這里給出一個簡單的Android App的示例,具體代碼可以從clone自[https://github.com/phodal/iot-android](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](http://lukencode.com/2010/04/27/calling-web-services-in-android-using-httpclient/) ### 創建RESTClient 在這里我們首先會定義四個REST方法GET、POST、PUT、DELETE ~~~ public void Execute(RequestMethod method) throws Exception { switch (method) { case GET: { // add parameters String combinedParams = ""; if (!params.isEmpty()) { combinedParams += "?"; for (NameValuePair p : params) { String paramString = p.getName() + "=" + URLEncoder.encode(p.getValue(), HTTP.UTF_8); if (combinedParams.length() > 1) { combinedParams += "&" + paramString; } else { combinedParams += paramString; } } } HttpGet request = new HttpGet(url + combinedParams); request.addHeader("Accept-Encoding", "gzip"); // add headers for (NameValuePair h : headers) { request.addHeader(h.getName(), h.getValue()); } executeRequest(request, url); break; } case POST: { HttpPost request = new HttpPost(url); request.addHeader("Accept-Encoding", "gzip"); // add headers for (NameValuePair h : headers) { request.addHeader(h.getName(), h.getValue()); } if (!data.equals("")) { request.setEntity(new StringEntity(data, HTTP.UTF_8)); } if (!params.isEmpty()) { request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); } executeRequest(request, url); break; } case PUT: { HttpPut request = new HttpPut(url); request.addHeader("Accept-Encoding", "gzip"); // add headers for (NameValuePair h : headers) { request.addHeader(h.getName(), h.getValue()); } if (!data.equals("")) { request.setEntity(new StringEntity(data, HTTP.UTF_8)); } if (!params.isEmpty()) { request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); } executeRequest(request, url); break; } case DELETE: { HttpDelete request = new HttpDelete(url); request.addHeader("Accept-Encoding", "gzip"); // add headers for (NameValuePair h : headers) { request.addHeader(h.getName(), h.getValue()); } executeRequest(request, url); break; } } } ~~~ 這四個方法最后都執行executeRequest來獲取響應結果。 ~~~ protected void executeRequest(HttpUriRequest request, String url) { HttpParams httpParameters = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); HttpProtocolParams.setUseExpectContinue(httpParameters, false); request.setParams(httpParameters); setOauth(request); DefaultHttpClient client = new DefaultHttpClient(); HttpResponse httpResponse; try { httpResponse = client.execute(request); responseCode = httpResponse.getStatusLine().getStatusCode(); message = httpResponse.getStatusLine().getReasonPhrase(); HttpEntity entity = httpResponse.getEntity(); if (entity != null) { InputStream instream = httpResponse.getEntity().getContent(); Header contentEncoding = httpResponse .getFirstHeader("Content-Encoding"); if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) { instream = new GZIPInputStream(instream); } // instream = entity.getContent(); response = convertStreamToString(instream); // Closing the input stream will trigger connection release instream.close(); } } catch (ClientProtocolException e) { client.getConnectionManager().shutdown(); e.printStackTrace(); } catch (IOException e) { client.getConnectionManager().shutdown(); e.printStackTrace(); } } ~~~ 接著,我們便可以執行getResponse()函數來獲取結果。 ## 使用REST Client獲取結果 使用RESTClient時,便可以用下面的示例 ~~~ RestClient client = new RestClient(tUrl); try { client.Execute(RequestMethod.GET); if (client.getResponseCode() != 200) { //do something } //JSONArray jArray = new JSONArray(client.getResponse()); } catch (Exception e) { //do something } ~~~ 而這時,我們只需要對相應的數據進行處理就可以了,如 ~~~ JSONArray jArray = new JSONArray(client.getResponse()); JSONObject jObj=jArray.getJSONObject(0); vshow.setText(jObj.toString()); outputJSON(jObj); ~~~ 將他轉換為String,接著在Android端上顯示最后的結果。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看