Android Http请求框架二:xUtils 框架网络请求
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); //设置超时时间 10s
http.send(HttpRequest.HttpMethod.GET,
url ,
new RequestCallBack<String>(){
@Override
public void onLoading(long total, long current, boolean isUploading) {
}
@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
result = responseInfo.result.toString() ;
}
@Override
public void onStart() {
}
@Override
public 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>() {
@Override
public void onStart() {
}
@Override
public void onLoading(long total, long current, boolean isUploading) {
}
@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
result = responseInfo.result.toString() ;
}
@Override
public 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>() {
@Override
public void onStart() {
}
@Override
public void onLoading(long total, long current, boolean isUploading) {
}
@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
result = responseInfo.result.toString() ;
}
@Override
public 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;
}
}
关注公众号
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
Android 中的Json解析工具fastjson 、序列化、反序列化
Android中通常需要访问服务器,然而服务器返回的数据很多时候都是Json格式 1、fastjson简介 阿里巴巴FastJson是一个Json处理工具包,包括“序列化”和“反序列化”两部分,它具备如下特征: 速度最快,测试表明,fastjson具有极快的性能,超越任其他的Java Json parser。包括自称最快的JackJson; 功能强大,完全支持JavaBean、集合、Map、日期、Enum,支持范型,支持自省;无依赖,能够直接运行在Java SE 5.0以上版本; 支持Android;开源 (Apache 2.0) 2、fastjson下载地址 gitHub 下载地址 : https://github.com/alibaba/fastjson Android jar包下载: http://repo1.maven.org/maven2/com/alibaba/fastjson/1.1.43.android/ 3、常用的方法 (1)将JsonArray字符串转化成 list 集合, 比如: JSONArray array = 。。。。 List<AD...
-
下一篇
Android 中的编码与解码
前言:今天遇到一个问题,一个用户在登录的时候,出现登录失败。但是其他用户登录都是正常的,经过调试发现登录失败的用户的密码中有两个特殊字符: * 、# 。 特殊符号在提交表单的时候,出现了编码不一样的问题。那么编码是什么鬼?? 1、什么是application/x-www-form-urlencoded字符串? 它是一种编码类型。 当URL地址里包含非西欧字符的字符串时,系统会将这些字符转换成application/x-www-form-urlencoded字符串。 表单提交时也是如此,当包含非西欧字符的字符串时,系统也会将这些字符转换成application/x-www-form-urlencoded字符串。 package com.app; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.net.URLEncoder; public class AA { public static void main(String[] args) { /** * 将a...
相关文章
文章评论
共有0条评论来说两句吧...

微信收款码
支付宝收款码