開篇報錯注意:本教程是基於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 }