IDC知識庫
IDC領域專業(yè)知識百科平臺

云服務器知識安卓如何訪問云服務器地址

安卓如何訪問服務器地址?使用安卓程序連接到云服務器,用httpClient或者okhttp 原理都是一樣,發(fā)起http請求傳遞數據,解析結果。這里我們根據百度知道網友的介紹,舉例一下 httpClient 的用法:

1. GET 方式傳遞參數

//先將參數放入List,再對參數進行URL編碼

List<BasicNameValuePair> params = new LinkedList<BasicNameValuePair>();

params.add(new BasicNameValuePair(“param1”, “數據”)); // 增加參數1

params.add(new BasicNameValuePair(“param2”, “value2”));// 增加參數2

String param = URLEncodedUtils.format(params, “UTF-8”);// 對參數編碼

String baseUrl = “服務器接口完整URL”;

HttpGet getMethod = new HttpGet(baseUrl + “?” + param);// 將URL與參數拼接

HttpClient httpClient = new DefaultHttpClient();

try {HttpResponse response = httpClient.execute(getMethod); // 發(fā)起GET請求

Log.i(TAG, “resCode = ” + response.getStatusLine().getStatusCode()); // 獲取響應碼

Log.i(TAG, “result = ” + EntityUtils.toString(response.getEntity(), “utf-8”));// 獲取服務器響應內容

} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e)

{e.printStackTrace();}

2. POST方式 方式傳遞參數

//和GET方式一樣,先將參數放入List

params = new LinkedList<BasicNameValuePair>();

params.add(new BasicNameValuePair(“param1”, “Post方法”));// 增加參數1

params.add(new BasicNameValuePair(“param2”, “第二個參數”));// 增加參數2

try {HttpPost postMethod = new HttpPost(baseUrl);// 創(chuàng)建一個post請求

postMethod.setEntity(new UrlEncodedFormEntity(params, “utf-8”)); // 將參數填入POST Entity中

HttpResponse response = httpClient.execute(postMethod); //執(zhí)行POST方法

Log.i(TAG, “resCode = ” + response.getStatusLine().getStatusCode()); // 獲取響應碼

Log.i(TAG, “result = ” + EntityUtils.toString(response.getEntity(), “utf-8”)); // 獲取響應內容

} catch (UnsupportedEncodingException e) {e.printStackTrace();

} catch (ClientProtocolException e) {e.printStackTrace();}

catch (IOException e) {e.printStackTrace();}

以上是關于安卓如何訪問云服務器地址的分享,域名頻道網站提供多類型、高性價的云服務器產品,云主機購買鏈接 http://forest-oxygen.cn/vps/

贊(7)
分享到: 更多 (0)

中國專業(yè)的網站域名及網站空間提供商

買域名買空間