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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android Http请求框架二:xUtils 框架网络请求

發布時間:2024/9/30 Android 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android Http请求框架二:xUtils 框架网络请求 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一:對Http不了解的請看

Android Http請求框架一:Get 和 Post 請求

?

二、正文

1、xUtils 下載地址

? ? github 下載地址? : https://github.com/wyouflf/xUtils

?

2、關于網絡請求的方法

package com.jike.shanglv.NetAndJson;import java.io.File;import com.lidroid.xutils.HttpUtils; import com.lidroid.xutils.exception.HttpException; import com.lidroid.xutils.http.RequestParams; import com.lidroid.xutils.http.ResponseInfo; import com.lidroid.xutils.http.ResponseStream; import com.lidroid.xutils.http.callback.RequestCallBack; import com.lidroid.xutils.http.client.HttpRequest; import com.lidroid.xutils.util.LogUtils;public class HttpUtil {String result = "" ;/*** Get請求 異步的* @param url 服務器地址* @param userkey * @param str* @param sign 簽名* @return*/public String xutilsGet( String url , String userkey , String str , String sign ){RequestParams params = new RequestParams();params.addQueryStringParameter("userkey", userkey );params.addQueryStringParameter("str", str );params.addQueryStringParameter("sign", sign );HttpUtils http = new HttpUtils();http.configCurrentHttpCacheExpiry(1000 * 10); //設置超時時間 10shttp.send(HttpRequest.HttpMethod.GET,url ,new RequestCallBack<String>(){@Overridepublic void onLoading(long total, long current, boolean isUploading) {}@Overridepublic void onSuccess(ResponseInfo<String> responseInfo) {result = responseInfo.result.toString() ;}@Overridepublic void onStart() {}@Overridepublic void onFailure(HttpException error, String msg) {}});return result ;}/*** Post請求 異步的* @param url* @param userkey* @param str* @param sign* @return*/public String xutilsPost( String url , String userkey , String str , String sign ){RequestParams params = new RequestParams();params.addQueryStringParameter("userkey", userkey );params.addQueryStringParameter("str", str );params.addQueryStringParameter("sign", sign );// 只包含字符串參數時默認使用BodyParamsEntity,// 類似于UrlEncodedFormEntity("application/x-www-form-urlencoded")。//params.addBodyParameter("name", "value");// 加入文件參數后默認使用MultipartEntity("multipart/form-data"),// 如需"multipart/related",xUtils中提供的MultipartEntity支持設置subType為"related"。// 使用params.setBodyEntity(httpEntity)可設置更多類型的HttpEntity(如:// MultipartEntity,BodyParamsEntity,FileUploadEntity,InputStreamUploadEntity,StringEntity)。// 例如發送json參數:params.setBodyEntity(new StringEntity(jsonStr,charset));HttpUtils http = new HttpUtils();http.configCurrentHttpCacheExpiry(1000 * 10); //設置超時時間 10s http.send(HttpRequest.HttpMethod.POST ,url ,params,new RequestCallBack<String>() {@Overridepublic void onStart() {}@Overridepublic void onLoading(long total, long current, boolean isUploading) {}@Overridepublic void onSuccess(ResponseInfo<String> responseInfo) {result = responseInfo.result.toString() ;}@Overridepublic void onFailure(HttpException error, String msg) {}});return result ;}/*** 帶上傳文件的 Post請求 異步的* @param url* @param userkey* @param str* @param sign* @param picString 文件的地址* @return*/public String xutilsFilePost( String url , String userkey , String str , String sign , String picString ){RequestParams params = new RequestParams();params.addQueryStringParameter("userkey", userkey );params.addQueryStringParameter("str", str );params.addQueryStringParameter("sign", sign );// 只包含字符串參數時默認使用BodyParamsEntity,// 類似于UrlEncodedFormEntity("application/x-www-form-urlencoded")。//params.addBodyParameter("name", "value");// 加入文件參數后默認使用MultipartEntity("multipart/form-data"),// 如需"multipart/related",xUtils中提供的MultipartEntity支持設置subType為"related"。// 使用params.setBodyEntity(httpEntity)可設置更多類型的HttpEntity(如:// MultipartEntity,BodyParamsEntity,FileUploadEntity,InputStreamUploadEntity,StringEntity)。// 例如發送json參數:params.setBodyEntity(new StringEntity(jsonStr,charset));params.addBodyParameter("picture", new File( picString )) ;com.lidroid.xutils.HttpUtils http = new com.lidroid.xutils.HttpUtils();http.send(HttpRequest.HttpMethod.POST ,url ,params,new RequestCallBack<String>() {@Overridepublic void onStart() {}@Overridepublic void onLoading(long total, long current, boolean isUploading) {}@Overridepublic void onSuccess(ResponseInfo<String> responseInfo) {result = responseInfo.result.toString() ;}@Overridepublic void onFailure(HttpException error, String msg) {}});return result ;}//-------------------以上的代碼 是 異步請求的, 以下的代碼是同步請求的-------------------------//
/*** Get同步請求 必須在異步塊兒中執行* @param url* @param userkey* @param str* @param sign* @return*/private String xutilsGetSync(String url , String userkey , String str , String sign ) {RequestParams params = new RequestParams();params.addQueryStringParameter("userkey", userkey );params.addQueryStringParameter("str", str );params.addQueryStringParameter("sign", sign );HttpUtils http = new HttpUtils() ;http.configCurrentHttpCacheExpiry(1000 * 10); //設置超時時間 try {ResponseStream responseStream = http.sendSync(HttpRequest.HttpMethod.GET,url , params ) ;//int statusCode = responseStream.getStatusCode();//Header[] headers = responseStream.getBaseResponse().getAllHeaders();return responseStream.readString();} catch (Exception e) {LogUtils.e(e.getMessage(), e);}return null;}/*** Post同步請求 必須在異步塊兒中執行* @param url* @param userkey* @param str* @param sign* @return*/private String xutilsPostSync(String url , String userkey , String str , String sign ) {RequestParams params = new RequestParams();params.addQueryStringParameter("userkey", userkey );params.addQueryStringParameter("str", str );params.addQueryStringParameter("sign", sign );HttpUtils http = new HttpUtils() ;http.configCurrentHttpCacheExpiry(1000 * 10); //設置超時時間 try {ResponseStream responseStream = http.sendSync(HttpRequest.HttpMethod.POST ,url , params ) ;//int statusCode = responseStream.getStatusCode();//Header[] headers = responseStream.getBaseResponse().getAllHeaders();return responseStream.readString();} catch (Exception e) {LogUtils.e(e.getMessage(), e);}return null;} }

?

總結

以上是生活随笔為你收集整理的Android Http请求框架二:xUtils 框架网络请求的全部內容,希望文章能夠幫你解決所遇到的問題。

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