| | |
| | | import com.hdl.photovoltaic.utils.LocalManageUtil; |
| | | import com.hdl.photovoltaic.utils.Md5Utils; |
| | | |
| | | import org.jetbrains.annotations.NotNull; |
| | | |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | import java.util.ArrayList; |
| | |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Objects; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | import io.reactivex.rxjava3.disposables.Disposable; |
| | | import io.reactivex.rxjava3.functions.Consumer; |
| | | import okhttp3.Call; |
| | | import okhttp3.Callback; |
| | | import okhttp3.ConnectionPool; |
| | | import okhttp3.FormBody; |
| | | import okhttp3.MediaType; |
| | | import okhttp3.OkHttpClient; |
| | |
| | | |
| | | public static HDLException getException(HDLException e) { |
| | | try { |
| | | return new HDLException(e.getCode(), getErrorSting(e.getCode()), e.getmExtra()); |
| | | String str = TextUtils.isEmpty(getErrorSting(e.getCode())) ? e.getMsg() : getErrorSting(e.getCode()); |
| | | return new HDLException(e.getCode(), str, e.getmExtra()); |
| | | } catch (Exception exception) { |
| | | return e; |
| | | } |
| | |
| | | return HDLApp.getInstance().getAppLocaleContext().getString(R.string.ssl_error); |
| | | case 1005: |
| | | return HDLApp.getInstance().getAppLocaleContext().getString(R.string.timeout_error); |
| | | case 4: |
| | | return HDLApp.getInstance().getAppLocaleContext().getString(R.string.signature_error_log_in_again); |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | |
| | | |
| | | return ""; |
| | | } |
| | | |
| | | private final OkHttpClient client = new OkHttpClient.Builder() |
| | | .connectTimeout(30, TimeUnit.SECONDS) |
| | | .readTimeout(30, TimeUnit.SECONDS) |
| | | .writeTimeout(30, TimeUnit.SECONDS) |
| | | // .connectionPool(new ConnectionPool(10, 5, TimeUnit.MINUTES)) // 连接池复用 |
| | | .build(); |
| | | /** |
| | | * 获取json资源用的 |
| | | * |
| | | * @param url 地址 |
| | | * @param callBack 回调 |
| | | */ |
| | | public void requestJsonHttpGet(String url, CloudCallBeak<String> callBack) { |
| | | public void requestJsonHttpGetSync(String url, CloudCallBeak<String> callBack) { |
| | | if (TextUtils.isEmpty(url)) { |
| | | if (callBack != null) { |
| | | callBack.onSuccess(""); |
| | | } |
| | | } |
| | | |
| | | HdlThreadLogic.runSubThread(new Runnable() { |
| | | @Override |
| | | public void run() { |
| | |
| | | }); |
| | | |
| | | } |
| | | public void requestJsonHttpGet(String url, CloudCallBeak<String> callBack) { |
| | | if (TextUtils.isEmpty(url)) { |
| | | if (callBack != null) { |
| | | callBack.onSuccess(""); |
| | | } |
| | | return; |
| | | } |
| | | |
| | | Request request = new Request.Builder() |
| | | .url(url) |
| | | .addHeader("Accept-Language", UserConfigManage.getInstance().getCurrentAppLanguage()) |
| | | .build(); |
| | | |
| | | //使用异步请求 enqueue() |
| | | client.newCall(request).enqueue(new Callback() { |
| | | @Override |
| | | public void onFailure(@NotNull Call call, @NotNull IOException e) { |
| | | if (callBack != null) { |
| | | // 切回主线程回调(因为 callBack 可能涉及 UI 操作) |
| | | HdlThreadLogic.runMainThread(new Runnable() { |
| | | @Override |
| | | public void run() { |
| | | callBack.onFailure(new HDLException(-20002, e.getMessage())); |
| | | |
| | | } |
| | | }); |
| | | |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void onResponse(@NotNull Call call, @NotNull Response response) { |
| | | try { // 自动关闭 |
| | | if (response.isSuccessful() && response.body() != null) { |
| | | String str = response.body().string(); |
| | | if (callBack != null) { |
| | | // ⭐ 切回主线程 |
| | | HdlThreadLogic.runMainThread(() -> |
| | | callBack.onSuccess(str) |
| | | ); |
| | | } |
| | | } else { |
| | | if (callBack != null) { |
| | | HdlThreadLogic.runMainThread(() -> |
| | | callBack.onSuccess("") |
| | | ); |
| | | } |
| | | } |
| | | } catch (IOException e) { |
| | | if (callBack != null) { |
| | | HdlThreadLogic.runMainThread(new Runnable() { |
| | | @Override |
| | | public void run() { |
| | | callBack.onFailure(new HDLException(-20002, e.getMessage())); |
| | | |
| | | } |
| | | }); |
| | | } |
| | | }finally { |
| | | response.close(); |
| | | } |
| | | } |
| | | }); |
| | | } |
| | | |
| | | /** |
| | | * 请求服务器(get) |
| | |
| | | public Disposable downLoadFile(String url, CloudCallBeak<ResponseBody> callBack) { |
| | | return HxHttp.builder() |
| | | .url(url) |
| | | .headers(HDLSmartHeader.IGNORE_SIGN_HEADER, 1) |
| | | .headers(HDLSmartHeader.IGNORE_SIGN_HEADER, 1).headers(HDLSmartHeader.NOAUTHENTICATION, 1) |
| | | .build() |
| | | .download() |
| | | .subscribe(new Consumer<ResponseBody>() { |