日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

android联网程序,android 联网 HttpClient

發布時間:2025/3/15 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android联网程序,android 联网 HttpClient 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

可以使用?Android?集成進來的?apache?中關于聯網的API。

HttpParams?:?保存Http請求設定的參數對象

HttpConnectionParams?:提供對Http連接參數進行設定的方法,比如?連接超時時間?等。

HttpClient?:發起Http連接請求的對象,

HttpResponse?:Http?請求返回的響應

最后,其實apache還是提供了釋放?連接資源的方法的,不過是埋得深了點。

httpClient.getConnectionManager().shutdown();

這個shutdown并不是將手機網絡斷掉,而是將建立Http連接請求時所分配的資源釋放掉

工具類

/*

* post請求

*/

public static String post(RequestVo vo){

//使用DefaultHttpClient創建HttpClient實例

DefaultHttpClient client = new DefaultHttpClient();

HttpPost post = new HttpPost(vo.context.getString(R.string.host).concat(vo.context.getString(vo.requestUrl)));

//構建HttpPost

HttpParams params = new BasicHttpParams();//

params = new BasicHttpParams();

HttpConnectionParams.setConnectionTimeout(params, 20000); //連接超時

HttpConnectionParams.setSoTimeout(params, 30*1000); //響應超時

post.setParams(params);

String str = null;

/** 保持會話Session **/

/** 設置Cookie **/

MyHttpCookies li = new MyHttpCookies(vo.context);

CookieStore cs = li.getuCookie();

/** 第一次請求App保存的Cookie為空,所以什么也不做,只有當APP的Cookie不為空的時。把請請求的Cooke放進去 **/

if (cs != null) {

client.setCookieStore(li.getuCookie());

System.out.println("session");

}

/** 保持會話Session end **/

try {

//將由Map存儲的參數轉化為鍵值參數

if(vo.requestDataMap!=null){

HashMap map = vo.requestDataMap;

ArrayList pairList = new ArrayList();

for(Map.Entry entry:map.entrySet()){

BasicNameValuePair pair = new BasicNameValuePair(entry.getKey(), entry.getValue());

pairList.add(pair);

}

//使用編碼構建Post實體

HttpEntity entity = new UrlEncodedFormEntity(pairList,"UTF-8");

//設置Post實體

post.setEntity(entity);

}

//執行Post方法

HttpResponse response = client.execute(post);//包含響應的狀態和返回的結果==

if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){

//獲取Cookie

li.setuCookie(client.getCookieStore());

//獲取返回實體

String result = EntityUtils.toString(response.getEntity(),"UTF-8");

Log.e(NetUtil.class.getSimpleName(), result);

try {

str = result;

} catch (Exception e) {

Log.e(NetUtil.class.getSimpleName(), e.getLocalizedMessage(),e);

}

return str;

}

} catch (ClientProtocolException e) {

Log.e(NetUtil.class.getSimpleName(), e.getLocalizedMessage(),e);

return null;

} catch (IOException e) {

Log.e(NetUtil.class.getSimpleName(), e.getLocalizedMessage(),e);

return null;

}

return null;

}

/**

* get請求

* @param vo

* @return

*/

public static String get(RequestVo vo, String url){

DefaultHttpClient client = new DefaultHttpClient();

HttpGet get = new HttpGet(url);

String str = null;

/** 保持會話Session **/

// /** 設置Cookie **/

MyHttpCookies li = new MyHttpCookies(vo.context);

CookieStore cs = li.getuCookie();

/** 第一次請求App保存的Cookie為空,所以什么也不做,只有當APP的Cookie不為空的時。把請請求的Cooke放進去 **/

if (cs != null) {

client.setCookieStore(li.getuCookie());

System.out.println("session");

}

// /** 保持會話Session end **/

try {

HttpResponse response = client.execute(get);

if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){

li.setuCookie(client.getCookieStore());

String result = EntityUtils.toString(response.getEntity(),"UTF-8");

Log.e(NetUtil.class.getSimpleName(), result);

try {

str = result;

} catch (Exception e) {

Log.e(NetUtil.class.getSimpleName(), e.getLocalizedMessage(),e);

}

return str;

}

} catch (ClientProtocolException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return null;

}

/**

* http請求的緩存和一些公用的參數

* @author llc

*

*/

public class MyHttpCookies {

/** 每頁數據顯示最大數 */

private static int pageSize = 10;

/** 當前會話后的cokie信息 */

private static CookieStore uCookie = null;

/** 公用的HTTP提示頭信息 */

private static Header[] httpHeader;

/** HTTP連接的網絡節點 */

private static String httpProxyStr;

/**http請求的公用url部分**/

public static String baseurl = "http://www.2cto.com /River";

/**上下文對象**/

Context context;

public MyHttpCookies(Context context){

this.context = context;

/** y設置請求頭 **/

/** y設置請求頭 **/

// Header[] header = {

// new BasicHeader("PagingRows", String.valueOf(pageSize)) };

// httpHeader = header;

}

/**

* 增加自動選擇網絡,自適應cmwap、CMNET、wifi或3G

*/

@SuppressWarnings("static-access")

public void initHTTPProxy() {

WifiManager wifiManager = (WifiManager) (context.getSystemService(context.WIFI_SERVICE));

if (!wifiManager.isWifiEnabled()) {

Uri uri = Uri.parse("content://telephony/carriers/preferapn"); // 獲取當前正在使用的APN接入點

Cursor mCursor =context. getContentResolver().query(uri, null, null, null,

null);

if (mCursor != null) {

mCursor.moveToNext(); // 游標移至第一條記錄,當然也只有一條

httpProxyStr = mCursor.getString(mCursor.getColumnIndex("proxy"));

}

} else {

httpProxyStr = null;

}

}

public int getPageSize() {

return pageSize;

}

public void setPageSize(int pageSize) {

this.pageSize = pageSize;

}

public CookieStore getuCookie() {

return uCookie;

}

public void setuCookie(CookieStore uCookie) {

this.uCookie = uCookie;

}

public Header[] getHttpHeader() {

return httpHeader;

}

public String getHttpProxyStr() {

return httpProxyStr;

}

}

注:

??1.??? 使用POST方式時,傳遞參數必須使用NameValuePair數組

2. ? ?使用GET方式時,通過URL傳遞參數,注意寫法

3. ? ?通過setEntity方法來發送HTTP請求

4. ? ?通過DefaultHttpClient 的 execute方法來獲取HttpResponse

5. ? ?通過getEntity()從Response中獲取內容

總結

以上是生活随笔為你收集整理的android联网程序,android 联网 HttpClient的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。