地址
https://github.com/hongyangAndroid/okhttputils
http://blog.csdn.net/lmj623565791/article/details/49734867/
導(dǎo)庫
compile 'com.zhy:okhttputils:2.6.2'
可能用到的權(quán)限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
GET
OkHttpUtils.get().url("http://www.imooc.com")
.addParams("username","hyman")
.addParams("password","123")
.build()
.execute(new StringCallback() {
@Override
public void onError(Request request, Exception e) {
}
@Override
public void onResponse(String response) {
//UI線程
textView.setText(response);
}
});
Post
OkHttpUtils.post().url("http://t2.refineit.cn:98/shequ/api/index.php?api=group&action=hot")
.addParams("vName","10")
.addParams("vCode","10")
.addParams("uuid","ffffffff-c53f-2161-72de-3aa90033c587")
.addParams("dInfo","ffffffff-c53f-2161-72de-3aa90033c587")
.addParams("aSign","md5")
.build().execute(new StringCallback() {
@Override
public void onError(Request request, Exception e) {
}
@Override
public void onResponse(String response) {
textView.setText(response);
}
});
帶文件的post請求
OkHttpUtils.post()
.url("http://t2.refineit.cn:98/shequ/api/index.php?api=photo&action=uploadPhoto")
.addFile("photo",file.getName(),file)
.addParams("vName","10")
.addParams("vCode","10")
.addParams("uuid","ffffffff-c53f-2161-72de-3aa90033c587")
.addParams("dInfo","ffffffff-c53f-2161-72de-3aa90033c587")
.addParams("aSign","md5")
.build().execute(new StringCallback() {
@Override
public void onError(Request request, Exception e) {
}
@Override
public void onResponse(String response) {
textView.setText(response);
}
});
下載文件
OkHttpUtils.get().url
("https://res.wx.qq.com/open/zh_CN/htmledition/res/dev/download/sdk" +
"/WXTicket_Android221cbf.zip")
.build().execute
(new FileCallBack(Environment.getExternalStorageDirectory().getAbsolutePath(),
"okhttptest.zip") {
@Override
public void inProgress(float progress) {
textView.setText(progress+"");
}
@Override
public void onError(Request request, Exception e) {
}
@Override
public void onResponse(File response) {
textView.setText("文件路徑:" + response.getAbsolutePath()
+ "\n文件名:" + response.getName());
}
});