You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
SmsForwarder/app/src/main/java/com/idormy/sms/forwarder/utils/UpdateAppHttpUtil.java

124 lines
4.0 KiB
Java

package com.idormy.sms.forwarder.utils;
import android.support.annotation.NonNull;
import android.util.Log;
import com.vector.update_app.HttpManager;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.FileCallBack;
import com.zhy.http.okhttp.callback.StringCallback;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;
public class UpdateAppHttpUtil implements HttpManager {
/**
* 异步get
*
* @param url get请求地址
* @param params get参数
* @param callBack 回调
*/
@Override
public void asyncGet(@NonNull String url, @NonNull Map<String, String> params, @NonNull final HttpManager.Callback callBack) {
Log.i("UpdateAppHttpUtil", "asyncGet" + url);
Map<String, String> headers = new HashMap<>();
headers.put("heada", "bb");
OkHttpUtils.get()
.url(url)
.headers(headers)
.params(params)
.build()
.execute(new StringCallback() {
@Override
public void onError(Call call, Response response, Exception e, int id) {
Log.i("UpdateAppHttpUtil", "err response" + response);
callBack.onError(validateError(e, response));
}
@Override
public void onResponse(String response, int id) {
Log.i("UpdateAppHttpUtil", "response" + response);
callBack.onResponse(response);
}
});
}
/**
* 异步post
*
* @param url post请求地址
* @param params post请求参数
* @param callBack 回调
*/
@Override
public void asyncPost(@NonNull String url, @NonNull Map<String, String> params, @NonNull final HttpManager.Callback callBack) {
Log.i("UpdateAppHttpUtil", "asyncPost" + url);
// params.put("gggg","hhhh");
Map<String, String> headers = new HashMap<>();
headers.put("heada", "bb");
OkHttpUtils.post()
.url(url)
.headers(headers)
.params(params)
.build()
.execute(new StringCallback() {
@Override
public void onError(Call call, Response response, Exception e, int id) {
callBack.onError(validateError(e, response));
}
@Override
public void onResponse(String response, int id) {
callBack.onResponse(response);
}
});
}
/**
* 下载
*
* @param url 下载地址
* @param path 文件保存路径
* @param fileName 文件名称
* @param callback 回调
*/
@Override
public void download(@NonNull String url, @NonNull String path, @NonNull String fileName, @NonNull final HttpManager.FileCallback callback) {
OkHttpUtils.get()
.url(url)
.build()
.execute(new FileCallBack(path, fileName) {
@Override
public void inProgress(float progress, long total, int id) {
callback.onProgress(progress, total);
}
@Override
public void onError(Call call, Response response, Exception e, int id) {
callback.onError(validateError(e, response));
}
@Override
public void onResponse(File response, int id) {
callback.onResponse(response);
}
@Override
public void onBefore(Request request, int id) {
super.onBefore(request, id);
callback.onBefore();
}
});
}
}