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方式

 1     public void xUtils_HttpUtilsGetString(String url) {
 2         //HttpUtils实例化对象
 3         HttpUtils http = new HttpUtils();
 4         /*
 5           *发送请求send(HttpMethod method, String url, RequestCallBack<T> callBack)
 6           * method请求方式
 7           * url请求地址
 8           *RequestCallBack <String>请求完后的回调监听String是请求完后你想让他返回什么类型的
 9          */
10         http.send(HttpRequest.HttpMethod.GET, url,
11                 new RequestCallBack<String>() {
12                     @Override
13                     public void onLoading(long total, long current, boolean isUploading) {
14                     }
15                     @Override
16                     public void onSuccess(ResponseInfo<String> responseInfo) {
17                         tvShow.setText(responseInfo.result);
18                     }
19                     @Override
20                     public void onStart() {
21                     }
22                     @Override
23                     public void onFailure(HttpException error, String msg) {
24                     }
25                 });
26     }

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", "20"); //添加请求参数
    //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的简单使用
 1     public void xUtilsLoadBitmap(String url) {
 2         //获得BitmapUtils的对象
 3         BitmapUtils bitmapUtils  = new BitmapUtils(this);
 4         bitmapUtils.configDefaultLoadingImage(R.mipmap.ic_launcher);//默认背景图片
 5         bitmapUtils.configDefaultLoadFailedImage(R.mipmap.ic_launcher);//加载失败图片
 6         bitmapUtils.configDefaultBitmapConfig(Bitmap.Config.RGB_565);//设置图片压缩类型
 7         // 加载网络图片
 8         bitmapUtils.display(image, url);
 9         // 加载本地图片(路径以/开头, 绝对路径)
10         //bitmapUtils.display(testImageView, "/sdcard/test.jpg");
11         // 加载assets中的图片(路径以assets开头)
12         //   bitmapUtils.display(testImageView, "assets/img/wallpaper.jpg");
13         // 使用ListView等容器展示图片时可通过PauseOnScrollListener控制滑动和快速滑动过程中时候暂停加载图片
14         // listView.setOnScrollListener(new PauseOnScrollListener(bitmapUtils, false, true));
15         //  listView.setOnScrollListener(new PauseOnScrollListener(bitmapUtils, false, true, customListener));
16     }
原文地址:https://www.cnblogs.com/lizhanqi/p/5714370.html