package com.rehome.zhdcoa.utils; import android.app.Activity; import android.content.Context; import android.content.pm.ApplicationInfo; import android.text.TextUtils; import android.util.Log; import android.widget.Toast; import com.google.gson.Gson; import com.rehome.zhdcoa.Api; import com.rehome.zhdcoa.Contans; import com.rehome.zhdcoa.R; import com.rehome.zhdcoa.base.BaseCallBack; import com.rehome.zhdcoa.bean.ResultBean; import com.rehome.zhdcoa.bean.RowsBean; import com.rehome.zhdcoa.bean.SubmitRecommendationsBean; import com.rehome.zhdcoa.ui.toastview.toastviewbymyself; import com.yolanda.nohttp.NoHttp; import com.yolanda.nohttp.RequestMethod; import com.yolanda.nohttp.rest.Request; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import okhttp3.MediaType; import okhttp3.RequestBody; import retrofit2.Call; import retrofit2.Response; /** * Created by Rehome-rjb1 on 2017/5/19. * 所有申请保存的工具类 */ public class SaveApplyUtils { //private final Api api; private static Builder builder; private Activity mContext; private SaveApplyUtils(Activity context) { //api = HttpUtils.getApi(context); this.mContext=context; init(); } private void init() { String url = Contans.BASE_URL + Contans.APPLY_SAVE_URL; Request request = NoHttp.createStringRequest(url, RequestMethod.POST); String jsonEncrypt = RSAUtils.encryptBASE64Str(builder.json); request.setDefineRequestBodyForJson(jsonEncrypt); NohttpUtils.getInstance().add(mContext, 0, "正在提交数据...", request, new HttpListener() { @Override public void onSucceed(int what, com.yolanda.nohttp.rest.Response response) { showLog("----------------"); String resultStr = response.get(); String jsonDecode = RSAUtils.decryptBASE64StrClient(resultStr); showLog(resultStr); showLog(jsonDecode); if (TextUtils.isEmpty(jsonDecode)) { showToast(UiUtlis.getString(mContext, R.string.data_error)); } else { ResultBean bean = GsonUtils.GsonToBean(jsonDecode, ResultBean.class); if (bean != null) { String result = bean.getRows().get(0).getResult(); String msg = bean.getRows().get(0).getMsg(); //1成功,0失败,2异常,3无权限 if (result.equals("1")) { Toast.makeText(builder.context, msg, Toast.LENGTH_SHORT).show(); builder.listener.onSuccess(); } else { Toast.makeText(builder.context, msg, Toast.LENGTH_SHORT).show(); builder.listener.onError(); } } } } @Override public void onFailed(int what, com.yolanda.nohttp.rest.Response response) { } }); // RequestBody body = RequestBody.create(MediaType.parse("application/json"), builder.json); // api.saveApply(body).enqueue(new BaseCallBack(builder.context) { // @Override // public void onSuccess(Call call, Response response) { // ResultBean bean = response.body(); // if (bean != null) { // String result = bean.getRows().get(0).getResult(); // String msg = bean.getRows().get(0).getMsg(); // Log.i("app",new Gson().toJson(bean)); // // // //1成功,0失败,2异常,3无权限 // if (result.equals("1")) { // Toast.makeText(builder.context, msg, Toast.LENGTH_SHORT).show(); // builder.listener.onSuccess(); // } else { // Toast.makeText(builder.context, msg, Toast.LENGTH_SHORT).show(); // builder.listener.onError(); // } // } // } // // @Override // public void onError(Call call, Throwable t) { // builder.listener.onError(); // } // }); } public static Builder newBuilder() { builder = new SaveApplyUtils.Builder(); return builder; } public static class Builder { private String json; private Activity context; private SaveApplyUtilsListener listener; public Builder setjson(String json) { this.json = json; return builder; } public Builder setListener(SaveApplyUtilsListener listener) { this.listener = listener; return builder; } public SaveApplyUtils builder(Activity context) { this.context = context; valida(); return new SaveApplyUtils(context); } private void valida() { if (this.context == null) { throw new RuntimeException("context不能为空"); } if (this.listener == null) { throw new RuntimeException("listener不能为空"); } if (TextUtils.isEmpty(this.json)) { throw new RuntimeException("json不能为空"); } } } public interface SaveApplyUtilsListener { void onSuccess(); void onError(); } public void showLog(String logText) { if (isApkInDebug(mContext)) { if(TextUtils.isEmpty(logText)){ Log.i("app", "logText is null"); }else{ Log.i("app", logText); } } } /** * 判断当前应用是否是debug状态 */ public static boolean isApkInDebug(Context context) { try { ApplicationInfo info = context.getApplicationInfo(); return (info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0; } catch (Exception e) { return false; } } public void showToast(String msg) { Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show(); } public void showToast(int strId) { Toast.makeText(mContext, strId, Toast.LENGTH_SHORT).show(); } }