一:对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;}}

更多相关文章

  1. android 自定义时间控件
  2. android am 启动activity service or broadcast及参数传递
  3. Android Studio第三十八期 - HIOS跳转协议解决URI跳转原生页面并
  4. android animation中的参数interpolator详解
  5. Android仿iPhone的日期时间选择器
  6. Android 自定义日期和时间和星期的弹窗

随机推荐

  1. 据说假设检验是个很难的题
  2. SQL今日一题(20):4个表
  3. Python爬虫-MongoDB
  4. 用Python搭建一个简单的代理池
  5. 指标又双叒下降了,我到底该怎么给老板分析
  6. 用Python识别图形验证码,实现自动登陆!
  7. KEY私钥格式转换——已有的RSA公私钥转换
  8. 100篇原创文章,可乐de数据分析之路
  9. 2020年出生人口会大跌吗?解读人口数据
  10. ssh 创建多个秘钥对,连接不同的服务器