主要代码如下:
/**
*初始化Httputils
*/
private void initHttpUtils() {
// 参数 联网超时时间
mHttpUtils=new HttpUtils(5000);
// 设置缓存大小
int size= (int) (Runtime.getRuntime().totalMemory()/8);
mHttpUtils.configHttpCacheSize(size);
// 设置字符集的格式 UTF-8
mHttpUtils.configResponseTextCharset("UTF-8");
// 设置重新联网的次数,如果连接服务器失败,重新联网的次数
mHttpUtils.configRequestRetryCount(3);
// 设置多线程并发执行
mHttpUtils.configRequestThreadPoolSize(4);
// 设置缓存数据失效的时间
// 设置30分钟后缓存数据失效
mHttpUtils.configDefaultHttpCacheExpiry(1000*60*30);
}
// 使用HttpUtils GET方式进行联网请求
public void btnHttpGet(View view) {
// 使用
String url= "http://mobile.ximalaya.com/mobile/discovery/v1/recommends?channel=and-f6&device=android&includeActivity=true&includeSpecial=true&scale=2&version=4.1.7.1";
mHttpUtils.send(HttpRequest.HttpMethod.GET, //参数1 联网请求方式
url, //参数2 请求地址
new RequestCallBack() {//参数3 回调
@Override
public void onSuccess(ResponseInfo responseInfo) {//联网成功的回调
Toast.makeText(MainActivity.this,"成功获取数据",Toast.LENGTH_LONG).show();
if (responseInfo != null) {
// 取出数据
String result = responseInfo.result;
Log.d("flag","---->Success"+result);
}
}
@Override
public void onFailure(HttpException error, String msg) {//联网失败的回调
Toast.makeText(MainActivity.this,"获取数据失败",Toast.LENGTH_LONG).show();
}
});
}
// HttpUtils进行Post请求
public void btnHttpPost(View view) {
String url="http://www.1000phone.net:8088/qfxl/index.php?s=appapi&a=topic";
RequestParams params=new RequestParams();
/**
*参数 是否必须 参数说明
uid 是 用户id
tid 否 1为精品关怀,2为近期关怀,默认为近期关怀
*/
// 服务器数据类型时String类型 值1-500都可以
params.addBodyParameter("uid","200");
params.addBodyParameter("tid","2");
mHttpUtils.send(HttpRequest.HttpMethod.POST,
url,
params,
new RequestCallBack() {
@Override
public void onSuccess(ResponseInfo responseInfo) {
Toast.makeText(MainActivity.this,"成功获取数据",Toast.LENGTH_LONG).show();
if (responseInfo != null) {
String result = responseInfo.result;
Log.d("flag","---->PostSuccess"+result);
}
}
@Override
public void onFailure(HttpException error, String msg) {
Toast.makeText(MainActivity.this,"获取数据失败",Toast.LENGTH_LONG).show();
}
});
}
// 使用HttpUtils上传头像
public void btnUploadImage(View view) {
// 上传文件就是Post请求
// 将想要上传的文件作为参数 传给Params
String url="http://www.1000phone.net:8088/qfxl/index.php?s=appapi&a=addalbum";
RequestParams params=new RequestParams();
/**
* 服务器请求参数要求:
参数 是否必须 参数说明
Token 否 用户登录令牌
uid 是 用户ID
file_{num} 是 form-data中媒体文件标识,有filename、filelength、content-type等信息,num>=0
count 是 上传图片数量
mobiles否 好友手机号(多个号码使用逗号(,)隔开)
desc 否 相册描述
返回结果
成功:{"status":"1","data":"上传成功!"}
失败:{"status":"0","data":"上传失败!"}
*/
// 添加要上传的文件
params.addBodyParameter("uid","178");
params.addBodyParameter("count","1");
String path=null;
// getExternalStorageDirectory() 这个是SD卡的跟路径
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
// path=Environment.getExternalStorageDirectory().getAbsolutePath()+
// File.separator+"Download"+
// File.separator+"images.jpg";
path=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath()+
File.separator+"images.jpg";
}
File file=new File(path);
if (file != null) {
Log.d("flag","---->"+file.length());
}
params.addBodyParameter("file_3",file);
mHttpUtils.send(HttpRequest.HttpMethod.POST, url, params, new RequestCallBack() {
@Override
public void onSuccess(ResponseInfo responseInfo) {
Toast.makeText(MainActivity.this,"上传文件成功",Toast.LENGTH_LONG).show();
if (responseInfo != null) {
String result = responseInfo.result;
Log.d("flag","----->Result"+result);
}
}
@Override
public void onFailure(HttpException error, String msg) {
Toast.makeText(MainActivity.this,"上传头像失败",Toast.LENGTH_LONG).show();
}
});
}