Android網(wǎng)絡(luò )編程之Http通信
Android中提供的HttpURLConnection和HttpClient接口可以用來(lái)開(kāi)發(fā)HTTP程序。以下是本人在學(xué)習中的總結與歸納。
本文引用地址:http://dyxdggzs.com/article/201609/305041.htm1. HttpURLConnection接口
首先需要明確的是,Http通信中的POST和GET請求方式的不同。GET可以獲得靜態(tài)頁(yè)面,也可以把參數放在URL字符串后面,傳遞給服務(wù)器。而POST方法的參數是放在Http請求中。因此,在編程之前,應當首先明確使用的請求方法,然后再根據所使用的方式選擇相應的編程方式。
HttpURLConnection是繼承于URLConnection類(lèi),二者都是抽象類(lèi)。其對象主要通過(guò)URL的openConnection方法獲得。創(chuàng )建方法如下代碼所示:
URL url = new URL(http://www.51cto.com/index.jsp?par=123456);
HttpURLConnection urlConn=(HttpURLConnection)url.openConnection();
過(guò)以下方法可以對請求的屬性進(jìn)行一些設置,如下所示:
//設置輸入和輸出流
urlConn.setDoOutput(true);
urlConn.setDoInput(true);
//設置請求方式為POST
urlConn.setRequestMethod(POST);
//POST請求不能使用緩存
urlConn.setUseCaches(false);
//關(guān)閉連接
urlConn.disConnection();
HttpURLConnection默認使用GET方式,例如下面代碼所示:
//使用HttpURLConnection打開(kāi)連接
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
//得到讀取的內容(流)
InputStreamReader in = new InputStreamReader(urlConn.getInputStream());
// 為輸出創(chuàng )建BufferedReader
BufferedReader buffer = new BufferedReader(in);
String inputLine = null;
//使用循環(huán)來(lái)讀取獲得的數據
while (((inputLine = buffer.readLine()) != null))
{
//我們在每一行后面加上一個(gè)n來(lái)?yè)Q行
resultData += inputLine + n;
}
//關(guān)閉InputStreamReader
in.close();
//關(guān)閉http連接
urlConn.disconnect();
如果需要使用POST方式,則需要setRequestMethod設置。代碼如下:
String httpUrl = http://192.168.1.110:8080/httpget.jsp;
//獲得的數據
String resultData = ;
URL url = null;
try
{
//構造一個(gè)URL對象
url = new URL(httpUrl);
}
catch (MalformedURLException e)
{
Log.e(DEBUG_TAG, MalformedURLException);
}
if (url != null)
{
try
{
// 使用HttpURLConnection打開(kāi)連接
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
//因為這個(gè)是post請求,設立需要設置為true
urlConn.setDoOutput(true);
urlConn.setDoInput(true);
// 設置以POST方式
urlConn.setRequestMethod(POST);
// Post 請求不能使用緩存
urlConn.setUseCaches(false);
urlConn.setInstanceFollowRedirects(true);
// 配置本次連接的Content-type,配置為application/x-www-form-urlencoded的
urlConn.setRequestProperty(Content-Type,application/x-www-form-urlencoded);
// 連接,從postUrl.openConnection()至此的配置必須要在connect之前完成,
// 要注意的是connection.getOutputStream會(huì )隱含的進(jìn)行connect。
urlConn.connect();
//DataOutputStream流
DataOutputStream out = new DataOutputStream(urlConn.getOutputStream());
//要上傳的參數
String content = par= + URLEncoder.encode(ABCDEFG, gb2312);
//將要上傳的內容寫(xiě)入流中
out.writeBytes(content);
//刷新、關(guān)閉
out.flush();
out.close();
2. HttpClient接口
使用Apache提供的HttpClient接口同樣可以進(jìn)行HTTP操作。
對于GET和POST請求方法的操作有所不同。GET方法的操作代碼示例如下:
// http地址
String httpUrl = http://192.168.1.110:8080/httpget.jsp?par=HttpClient_android_Get;
//HttpGet連接對象
HttpGet httpRequest = new HttpGet(httpUrl);
//取得HttpClient對象
HttpClient httpclient = new DefaultHttpClient();
//請求HttpClient,取得HttpResponse
HttpResponse httpResponse = httpclient.execute(httpRequest);
//請求成功
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
{
//取得返回的字符串
String strResult = EntityUtils.toString(httpResponse.getEntity());
mTextView.setText(strResult);
}
else
{
mTextView.setText(請求錯誤!);
}
}
使用POST方法進(jìn)行參數傳遞時(shí),需要使用NameValuePair來(lái)保存要傳遞的參數。,另外,還需要設置所使用的字符集。代碼如下所示:
// http地址
String httpUrl = http://192.168.1.110:8080/httpget.jsp;
//HttpPost連接對象
HttpPost httpRequest = new HttpPost(httpUrl);
//使用NameValuePair來(lái)保存要傳遞的Post參數
List
//添加要傳遞的參數
params.add(new BasicNameValuePair(par, HttpClient_android_Post));
//設置字符集
HttpEntity httpentity = new UrlEncodedFormEntity(params, gb2312);
評論