use of com.okhttplib.HttpInfo in project OkHttp3 by MrZhousf.
the class MainActivity method async.
/**
* 异步请求:回调方法可以直接操作UI
*/
private void async() {
OkHttpUtil.getDefault(this).doGetAsync(HttpInfo.Builder().setUrl(url).addHead("head", //添加头参数
"test").addParam("param", //添加接口参数
"test").build(), new Callback() {
@Override
public void onFailure(HttpInfo info) throws IOException {
String result = info.getRetDetail();
resultTV.setText("异步请求失败:" + result);
}
@Override
public void onSuccess(HttpInfo info) throws IOException {
String result = info.getRetDetail();
resultTV.setText("异步请求成功:" + result);
//GSon解析
TimeAndDate time = new Gson().fromJson(result, TimeAndDate.class);
LogUtil.d("MainActivity", time.getResult().toString());
setFromCacheTV(info);
}
});
needDeleteCache(true);
}
use of com.okhttplib.HttpInfo in project OkHttp3 by MrZhousf.
the class MainActivity method sync.
/**
* 同步请求:由于不能在UI线程中进行网络请求操作,所以采用子线程方式
*/
private void sync() {
new Thread(new Runnable() {
@Override
public void run() {
final HttpInfo info = HttpInfo.Builder().setUrl(url).setResponseEncoding(//设置服务器响应编码
Encoding.UTF_8).build();
OkHttpUtil.getDefault(this).doGetSync(info);
final String result = info.getRetDetail();
runOnUiThread(new Runnable() {
@Override
public void run() {
resultTV.setText("同步请求:" + result);
setFromCacheTV(info);
}
});
}
}).start();
needDeleteCache(true);
}
use of com.okhttplib.HttpInfo in project OkHttp3 by MrZhousf.
the class MainActivity method networkThenCache.
/**
* 先网络再缓存:先请求网络,失败则请求缓存
*/
private void networkThenCache() {
OkHttpUtil.Builder().setCacheType(CacheType.NETWORK_THEN_CACHE).build(this).doGetAsync(HttpInfo.Builder().setUrl(url).build(), new Callback() {
@Override
public void onSuccess(HttpInfo info) throws IOException {
String result = info.getRetDetail();
resultTV.setText("NETWORK_THEN_CACHE:" + result);
setFromCacheTV(info);
}
@Override
public void onFailure(HttpInfo info) throws IOException {
resultTV.setText("NETWORK_THEN_CACHE:" + info.getRetDetail());
}
});
needDeleteCache(true);
}
use of com.okhttplib.HttpInfo in project OkHttp3 by MrZhousf.
the class UploadImageActivity method uploadImgOne.
/**
* 上传第一张图片
*/
private void uploadImgOne() {
HttpInfo info = HttpInfo.Builder().setUrl(url).addUploadFile("uploadFile", filePathOne, new ProgressCallback() {
//onProgressMain为UI线程回调,可以直接操作UI
@Override
public void onProgressMain(int percent, long bytesWritten, long contentLength, boolean done) {
uploadProgressOne.setProgress(percent);
LogUtil.d(TAG, "上传进度1:" + percent);
}
}).build();
doUploadImg(info);
}
use of com.okhttplib.HttpInfo in project OkHttp3 by MrZhousf.
the class UploadImageActivity method uploadImgMulti.
/**
* 批量上传:一次性批量上传请使用UploadFileActivity中的doUploadBatch方法
*/
private void uploadImgMulti() {
HttpInfo info = HttpInfo.Builder().setUrl(url).addUploadFile("file", filePathOne, new ProgressCallback() {
@Override
public void onProgressMain(int percent, long bytesWritten, long contentLength, boolean done) {
uploadProgressOne.setProgress(percent);
LogUtil.d(TAG, "上传进度1:" + percent);
}
@Override
public void onResponseMain(String filePath, HttpInfo info) {
LogUtil.d(TAG, "上传结果1:\n" + filePath + "\n" + info.getRetDetail());
}
}).addUploadFile("file", filePathTwo, new ProgressCallback() {
@Override
public void onProgressMain(int percent, long bytesWritten, long contentLength, boolean done) {
uploadProgressTwo.setProgress(percent);
LogUtil.d(TAG, "上传进度2:" + percent);
}
@Override
public void onResponseMain(String filePath, HttpInfo info) {
LogUtil.d(TAG, "上传结果2:\n" + filePath + "\n" + info.getRetDetail());
}
}).build();
doUploadImg(info);
}
Aggregations