xutils的HttpUtils,Post和Get基本使用,以及BitmapUtils的简单使用

时间:2023-03-09 22:33:09
xutils的HttpUtils,Post和Get基本使用,以及BitmapUtils的简单使用

开篇报错注意:本教程是基于xUtils-2.6.14.jar版本实现的

由于studio中6.0以后安卓取消了httpclient,而xutils则基于httpclient开发的,所以现在无法使用,将会有以下的错误

Error:(55, 30) 错误: 无法访问HttpRequestBase

找不到org.apache.http.client.methods.HttpRequestBase的类文件
Error:(85, 30) 错误: 无法访问HttpEntityEnclosingRequest
找不到org.apache.http.HttpEntityEnclosingRequest的类文件
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
2 个错误
:app:compileDebugJavaWithJavac FAILED

解决方案:在使用xutils的modle的build.gradle的  android的下添加

这句话:useLibrary 'org.apache.http.legacy'    即可解决

HttpUtilsGet方式

     public void xUtils_HttpUtilsGetString(String url) {
//HttpUtils实例化对象
HttpUtils http = new HttpUtils();
/*
*发送请求send(HttpMethod method, String url, RequestCallBack<T> callBack)
* method请求方式
* url请求地址
*RequestCallBack <String>请求完后的回调监听String是请求完后你想让他返回什么类型的
*/
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) {
tvShow.setText(responseInfo.result);
}
@Override
public void onStart() {
}
@Override
public void onFailure(HttpException error, String msg) {
}
});
}

HttpUtilsPost方式

public void xUtils_HttpUtilsPostString(String url) {
//RequestParams对象是用来存放请求参数的
RequestParams params = new RequestParams();
//例如:"http://www.sciencenet.cn/xml/iphoneinterface.aspx?type=news&nums=20"
    //params.addHeader("name","value”);//如果需要添加特殊的请求头可以使用这个
       params.addBodyParameter("type", "news");//添加请求参数
params.addBodyParameter("nums", ""); //添加请求参数
    //HttpUtils实例化对象 HttpUtils http = new HttpUtils(); 
    //发送请求/** *send(HttpMethod method, String url, RequestParams params, RequestCallBack<T> callBack) * method请求方法,url请求路径,params请求需要携带的参数,RequestCallBack成功后的回调方法 */
    http.send(HttpRequest.HttpMethod.POST, url, params, new RequestCallBack<String>() {
     @Override
    publicvoid onSuccess(ResponseInfo<String> responseInfo) {
    Log.i(TAG, "xUtils_HttpUtilsPostString...onSuccess: "+responseInfo.result);
      tvShow.setText("xUtils_HttpUtilsPostString"+responseInfo.result);
     }
    @Override
    publicvoid onFailure(HttpException e, String s) {
    Toast.makeText(MainActivity.this, "xUtils_HttpUtilsPostString加载失败", Toast.LENGTH_SHORT).show();
    Log.e(TAG, "xUtils_HttpUtilsPostString....onFailure: "+e );
    }});
}
BitmapUtils的简单使用
     public void xUtilsLoadBitmap(String url) {
//获得BitmapUtils的对象
BitmapUtils bitmapUtils = new BitmapUtils(this);
bitmapUtils.configDefaultLoadingImage(R.mipmap.ic_launcher);//默认背景图片
bitmapUtils.configDefaultLoadFailedImage(R.mipmap.ic_launcher);//加载失败图片
bitmapUtils.configDefaultBitmapConfig(Bitmap.Config.RGB_565);//设置图片压缩类型
// 加载网络图片
bitmapUtils.display(image, url);
// 加载本地图片(路径以/开头, 绝对路径)
//bitmapUtils.display(testImageView, "/sdcard/test.jpg");
// 加载assets中的图片(路径以assets开头)
// bitmapUtils.display(testImageView, "assets/img/wallpaper.jpg");
// 使用ListView等容器展示图片时可通过PauseOnScrollListener控制滑动和快速滑动过程中时候暂停加载图片
// listView.setOnScrollListener(new PauseOnScrollListener(bitmapUtils, false, true));
// listView.setOnScrollListener(new PauseOnScrollListener(bitmapUtils, false, true, customListener));
}