Android开发之XUtils框架使用和报错处理

 一、XUtils  lib的的添加:

1、点击+,选择第一个Library dependency

2、输入XUtils 按enter键,搜索:

3、然后就是选择XUtils,选择哪个版本就看个人了,接着点击ok-->ok,程序开始重新编译。

4、以HTTP请求为例:

 1 private void getDataFromServer(){
 2         HttpUtils utils = new HttpUtils();
 3         utils.send(HttpRequest.HttpMethod.GET, GlobalConstants.GATEGORY_URL, new RequestCallBack<String>() {
 4             @Override//请求成功
 5             public void onSuccess(ResponseInfo<String> responseInfo) {
 6                 String result = responseInfo.result;
 7                 System.out.println("result: "+result);
 8 
 9             }
10             @Override//请求失败
11             public void onFailure(HttpException error, String msg) {
12 
13             }
14         });
15 
16     }

上面的请求可能会报错,错误如下:

当遇到上面的错误是,你需要在build.gradle文件中添加一行代码(useLibrary 'org.apache.http.legacy'):

然后再编译就没问题了。

GitHub:https://github.com/godfunc
博客园:http://www.cnblogs.com/godfunc
Copyright ©2019 Godfunc
原文地址:https://www.cnblogs.com/Godfunc/p/6085047.html