一、導入控件
我使用的是 xutils2.6.14,導入方法如下,下載 xutils2.6.14(下載地址:)到目錄libs下
在app文件夾下的 build.gradle 文件添加如下依賴項:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:25.3.1'
# 關鍵是這個
compile files('libs/xUtils-2.6.14.jar')
compile 'com.google.android.gms:play-services-appindexing:8.1.0'
}
至此,再將頁面切換到對應的java文件,此時會彈出是術 “sync now” 點中即可
二、網絡請求示例
直接上代碼
private void test() {
//請求參數定義
RequestParams params = new RequestParams();
params.addBodyParameter("index", "1");
params.addBodyParameter("param1", "test);
HttpUtils https = new HttpUtils();
https.configCurrentHttpCacheExpiry(2000);
//此爲請求地址,根據具體定義
String url = "https://www.baidu.com";
HttpHandler<String> send = https.send(HttpRequest.HttpMethod.POST, url, params,
new RequestCallBack<String>() {
@Override
public void onFailure(HttpException error, String msg) {
//失敗
LogUtil.logcat(Constants.SERVICE_DATASOURCE + ":" + "讀取啓動圖片錯誤,錯誤信息:" + msg + "\r\n異常信息:" + error.toString());
}
@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
//請求成功
if(responseInfo == null){
LogUtil.logcat(Constants.SERVICE_DATASOURCE + ":" + "responseInfo is null");
}else {
LogUtil.logcat(Constants.SERVICE_DATASOURCE + ":" + responseInfo.result);
}
}
);
}
三、過程中出現的問題
在寫好代碼,連接好編譯器後,點運行,上報如下錯誤
Error:(57, 58) 錯誤: 無法訪問HttpRequestBase
找不到org.apache.http.client.methods.HttpRequestBase的類文件
解決方法:在app下的build.gradle 中的 android 子集中新增依賴,如下所示
android{
.... 略
useLibrary 'org.apache.http.legacy'
}
以後再運行,一切OK。