日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android联网程序,android 联网 HttpClient

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

可以使用?Android?集成進來的?apache?中關(guān)于聯(lián)網(wǎng)的API。

HttpParams?:?保存Http請求設(shè)定的參數(shù)對象

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

HttpClient?:發(fā)起Http連接請求的對象,

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

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

httpClient.getConnectionManager().shutdown();

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

工具類

/*

* post請求

*/

public static String post(RequestVo vo){

//使用DefaultHttpClient創(chuàng)建HttpClient實例

DefaultHttpClient client = new DefaultHttpClient();

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

//構(gòu)建HttpPost

HttpParams params = new BasicHttpParams();//

params = new BasicHttpParams();

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

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

post.setParams(params);

String str = null;

/** 保持會話Session **/

/** 設(shè)置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存儲的參數(shù)轉(zhuǎn)化為鍵值參數(shù)

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);

}

//使用編碼構(gòu)建Post實體

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

//設(shè)置Post實體

post.setEntity(entity);

}

//執(zhí)行Post方法

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

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 **/

// /** 設(shè)置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請求的緩存和一些公用的參數(shù)

* @author llc

*

*/

public class MyHttpCookies {

/** 每頁數(shù)據(jù)顯示最大數(shù) */

private static int pageSize = 10;

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

private static CookieStore uCookie = null;

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

private static Header[] httpHeader;

/** HTTP連接的網(wǎng)絡(luò)節(jié)點 */

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設(shè)置請求頭 **/

/** y設(shè)置請求頭 **/

// Header[] header = {

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

// httpHeader = header;

}

/**

* 增加自動選擇網(wǎng)絡(luò),自適應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方式時,傳遞參數(shù)必須使用NameValuePair數(shù)組

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

3. ? ?通過setEntity方法來發(fā)送HTTP請求

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

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

總結(jié)

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。