當前位置:編程學習大全網 - 遊戲軟體 - 如何在Android開發中用HttpClient連接網絡數據

如何在Android開發中用HttpClient連接網絡數據

HttpClient網絡訪問

壹、HttpClient網絡訪問:

(壹)、簡介:

1、Apache組織提供了HttpClient項目,可以實現網絡訪問。在Android中,成功集成了HttpClient,所以在Android中可以直接使用HttpClient訪問網絡。

2、與HttpURLConnection相比,HttpClient將前者中的輸入、輸出流操作,統壹封裝成HttpGet、HttpPost、HttpRequest類。

HttpClient:網絡連接對象;

HttpGet:代表發送GET請求;

HttpPost:代表發送POST請求;

HttpResponse:代表處理服務器響應的對象。

HttpEntity對象:該對象中包含了服務器所有的返回內容。

3、使用步驟:(六部曲)重點

創建HttpClient對象:通過實例化DefaultHttpClient獲得;

創建HttpGet或HttpPost對象:通過實例化 HttpGet或HttpPost 獲得,而構造方法的參數是urlstring(即需要訪問的網絡url地址)。也可以通過調用setParams()方法來添加請求參數;

調用HttpClient對象的execute()方法,參數是剛才創建的 HttpGet或HttpPost對象 ,返回值是HttpResponse對象;

通過response對象中的getStatusLine()方法和getStatusCode()方法獲取服務器響應狀態是否是200。

調用 HttpResponse對象的getEntity()方法,返回HttpEntity對象。而該對象中包含了服務器所有的返回內容。

借助EntityUtils的toString()方法或toByteArray()對 HttpEntity對象進行處理,也可以通過IO流對 HttpEntity對象進行操作。

(二)、封裝HttpClientHelper工具類:

public class HttpClientHelper {

public static HttpClient checkNetwork(String url) {

HttpClient httpClient = new DefaultHttpClient();

HttpGet request = new HttpGet(url);

HttpResponse httpResponse = null;

try {

httpResponse = httpClient.execute(request);

if (httpResponse.getStatusLine().getStatusCode() == 200) {

return httpClient;

}

} catch (ClientProtocolException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return null;

}

/**

* 作用:實現網絡訪問文件,將獲取到數據儲存在文件流中

*

* @param url

* :訪問網絡的url地址

* @return inputstream

*/

public static InputStream loadFileFromURL(String url) {

HttpClient httpClient = new DefaultHttpClient();

HttpGet requestGet = new HttpGet(url);

HttpResponse httpResponse;

try {

httpResponse = httpClient.execute(requestGet);

if (httpResponse.getStatusLine().getStatusCode() == 200) {

HttpEntity entity = httpResponse.getEntity();

return entity.getContent();

}

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

/**

* 作用:實現網絡訪問文件,將獲取到的數據存在字節數組中

*

* @param url

* :訪問網絡的url地址

* @return byte[]

*/

public static byte[] loadByteFromURL(String url) {

HttpClient httpClient = new DefaultHttpClient();

HttpGet requestGet = new HttpGet(url);

try {

HttpResponse httpResponse = httpClient.execute(requestGet);

if (httpResponse.getStatusLine().getStatusCode() == 200) {

HttpEntity httpEntity = httpResponse.getEntity();

return EntityUtils.toByteArray(httpEntity);

}

} catch (Exception e) {

e.printStackTrace();

System.out.println("====>" + e.toString());

}

return null;

}

/**

* 作用:實現網絡訪問文件,返回字符串

*

* @param url

* :訪問網絡的url地址

* @return String

*/

public static String loadTextFromURL(String url) {

HttpClient httpClient = new DefaultHttpClient();

HttpGet requestGet = new HttpGet(url);

try {

HttpResponse httpResponse = httpClient.execute(requestGet);

if (httpResponse.getStatusLine().getStatusCode() == 200) {

HttpEntity httpEntity = httpResponse.getEntity();

return EntityUtils.toString(httpEntity, "utf-8");

}

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

/**

* 作用:實現網絡訪問文件,先給服務器通過“GET”方式提交數據,再返回相應的數據

*

* @param url

* :訪問網絡的url地址

* @param params

* String url:訪問url時,需要傳遞給服務器的參數。

* 第二個參數格式為:username=wangxiangjun&password=123456

* @return byte[]

*/

public static byte[] doGetSubmit(String url, String params) {

HttpClient httpClient = new DefaultHttpClient();

HttpGet requestGet = new HttpGet(url + "?" + params);

try {

HttpResponse httpResponse = httpClient.execute(requestGet);

if (httpResponse.getStatusLine().getStatusCode() == 200) {

HttpEntity httpEntity = httpResponse.getEntity();

return EntityUtils.toByteArray(httpEntity);

}

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

/**

* 作用:實現網絡訪問文件,先給服務器通過“POST”方式提交數據,再返回相應的數據

*

* @param url

* :訪問網絡的url地址

* @param params

* String url:訪問url時,需要傳遞給服務器的參數。 第二個參數為:List<NameValuePair>

* @return byte[]

*/

public static byte[] doPostSubmit(String url, List<NameValuePair> params) {

HttpClient httpClient = new DefaultHttpClient();

HttpPost requestPost = new HttpPost(url);

try {

requestPost.setEntity(new UrlEncodedFormEntity(params, "utf-8"));

HttpResponse httpResponse = httpClient.execute(requestPost);

if (httpResponse.getStatusLine().getStatusCode() == 200) {

HttpEntity httpEntity = httpResponse.getEntity();

return EntityUtils.toByteArray(httpEntity);

}

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

/**

* 作用:實現網絡訪問文件,先給服務器通過“POST”方式提交數據,再返回相應的數據

*

* @param url

* :訪問網絡的url地址

* @param params

* String url:訪問url時,需要傳遞給服務器的參數。 Map<String , Object>

* @return byte[]

*/

public static byte[] doPostSubmit(String url, Map<String, Object> params) {

HttpClient httpClient = new DefaultHttpClient();

HttpPost requestPost = new HttpPost(url);

List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>();

try {

if (params != null) {

for (Map.Entry<String, Object> entry : params.entrySet()) {

String key = entry.getKey();

String value = entry.getValue().toString();

BasicNameValuePair nameValuePair = new BasicNameValuePair(

key, value);

parameters.add(nameValuePair);

}

}

requestPost

.setEntity(new UrlEncodedFormEntity(parameters, "utf-8"));

HttpResponse httpResponse = httpClient.execute(requestPost);

if (httpResponse.getStatusLine().getStatusCode() == 200) {

HttpEntity httpEntity = httpResponse.getEntity();

return EntityUtils.toByteArray(httpEntity);

}

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

}

  • 上一篇:魚類的名字有哪些
  • 下一篇:妳見過哪些人從網紅變成了明星?
  • copyright 2024編程學習大全網