Search in sources :

Example 1 with StringCallback

use of com.zhy.http.okhttp.callback.StringCallback in project Mvp-Rxjava-Retrofit-dagger2 by pengMaster.

the class LoginUtils method initDeviceValid.

/**
 * @param isTestIn true 测试入口的回调
 *                 验证设备是否有效:
 *                 过期/不可用 0
 *                 即将过期    1
 */
public LoginUtils initDeviceValid(LoadMode isTestIn, final Context context, String type) {
    if (isTestIn == LoadMode.TEST && onInitDeviceValidCallListener != null) {
        onInitDeviceValidCallListener.onTestIn();
        return instance;
    }
    if (isAvilible("com.mtm.launcher2", "com.mtm.launcher3")) {
        StringBuilder sb = new StringBuilder();
        String deviceId = Md5Util.getMD5Str(getDeviceId());
        final int[] versionCode = { getAppVersionCode() };
        sb.append("http://60.247.49.230:7001/mtmdp/");
        // .append("type=").append(appName).append("&imei=").append(deviceId);
        sb.append("scjAction!getNewVersion.do?");
        OkHttpUtils.post().url(sb.toString()).addParams("type", type).addParams("imei", deviceId).build().execute(new StringCallback() {

            @Override
            public void onError(com.squareup.okhttp.Request request, Exception e) {
                if (onInitDeviceValidCallListener != null) {
                    if ("0".equals(SPUtils.i("deviceValid").getStringData("isDeviceValid", ""))) {
                        PopupWindow.showInfoDialog(context, "提示", "该设备服务不可用,请联系'系统服务商'.");
                        onInitDeviceValidCallListener.onError();
                    } else {
                        onInitDeviceValidCallListener.onResponse(0);
                    }
                }
                e.printStackTrace();
            }

            @Override
            public void onResponse(String response) {
                if (!TextUtils.isEmpty(response)) {
                    switch(response.trim().replaceAll("\r", "").replaceAll("\n", "")) {
                        case "/Exception":
                        case "/noApp":
                        case "/noDevice":
                        case // 过期/不可用
                        "/noUsed":
                            PopupWindow.showInfoDialog(context, "提示", "该设备服务不可用,请联系'系统服务商'.");
                            versionCode[0] = 0;
                            // 无效
                            SPUtils.i("deviceValid").saveStringData("isDeviceValid", "0");
                            break;
                        case // 即将过期
                        "/Warning":
                            versionCode[0] = 1;
                            new AlertDialog.Builder(context).setTitle("提示").setMessage("该设备服务即将过期,请联系'系统服务商'.").setCancelable(false).setPositiveButton("确定", new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog, int id) {
                                    dialog.dismiss();
                                    if (onInitDeviceValidCallListener != null) {
                                        onInitDeviceValidCallListener.onResponse(versionCode[0]);
                                    }
                                }
                            }).show();
                            // 即将过期
                            SPUtils.i("deviceValid").saveStringData("isDeviceValid", "2");
                            return;
                        default:
                            InternalVersionBean internalVersionBean = new Gson().fromJson(response, InternalVersionBean.class);
                            if (internalVersionBean != null && !TextUtils.isEmpty(internalVersionBean.getInternalVersion())) {
                                versionCode[0] = Integer.valueOf(internalVersionBean.getInternalVersion());
                                // 有效
                                SPUtils.i("deviceValid").saveStringData("isDeviceValid", "1");
                            }
                            break;
                    }
                }
                if (onInitDeviceValidCallListener != null) {
                    if (versionCode[0] == 0 || "0".equals(SPUtils.i("deviceValid").getStringData("isDeviceValid", ""))) {
                        // 无效
                        PopupWindow.showInfoDialog(context, "提示", "该设备服务不可用,请联系'系统服务商'.");
                        onInitDeviceValidCallListener.onError();
                    } else {
                        onInitDeviceValidCallListener.onResponse(versionCode[0]);
                    }
                }
            }
        });
    } else {
        ThreadUtils.runInMainThread(new Runnable() {

            @Override
            public void run() {
                PopupWindow.showInfoDialog(context, "提示", "请安装安全认证中心后重试!");
            }
        });
    }
    return instance;
}
Also used : DialogInterface(android.content.DialogInterface) StringCallback(com.zhy.http.okhttp.callback.StringCallback) Gson(com.google.gson.Gson) InternalVersionBean(com.mtm.mrecord.mvp.model.entity.InternalVersionBean)

Example 2 with StringCallback

use of com.zhy.http.okhttp.callback.StringCallback in project DMGameApp by xiaohaibin.

the class GetForumListPresenter method getForumListData.

@Override
public void getForumListData(String fid) {
    OkHttpUtils.postString().content(GsonUtil.newGson().toJson(new ForumContentEntity(fid, "forums"))).url(API.USER_API).build().execute(new StringCallback() {

        @Override
        public void onBefore(Request request, int id) {
            getView().showLoading();
        }

        @Override
        public void onError(Call call, Exception e, int id) {
            getView().getForumListDataFailed(e.getMessage());
        }

        @Override
        public void onResponse(String response, int id) {
            if (!TextUtils.isEmpty(response)) {
                try {
                    JsonResponse jsonResponse = new JsonResponse(new JSONObject(response));
                    if (jsonResponse.isSuccess()) {
                        List<ForumBean> dataList = new ArrayList<>();
                        JSONArray array = jsonResponse.getDataList();
                        JSONObject object = jsonResponse.getObject();
                        if (array != null && object != null) {
                            for (int i = 0; i < array.length(); i++) {
                                ForumBean forumBean = new ForumBean();
                                JSONObject jsonObject = object.getJSONObject(array.getString(i));
                                forumBean.setFid(jsonObject.getString("fid"));
                                forumBean.setName(jsonObject.getString("name"));
                                forumBean.setTodayposts(jsonObject.getString("todayposts"));
                                forumBean.setRank(jsonObject.getString("rank"));
                                forumBean.setType(jsonObject.getString("type"));
                                forumBean.setIcon(jsonObject.getString("icon"));
                                dataList.add(forumBean);
                            }
                            getView().getForumListDataSuccess(dataList);
                        } else {
                            getView().hideLoading();
                            getView().getForumListDataFailed(jsonResponse.getMsg());
                        }
                    } else {
                        getView().hideLoading();
                        getView().getForumListDataFailed(jsonResponse.getMsg());
                    }
                } catch (JSONException e) {
                    getView().hideLoading();
                    getView().getForumListDataFailed(e.getMessage());
                }
            }
        }

        @Override
        public void onAfter(int id) {
            getView().hideLoading();
        }
    });
}
Also used : Call(okhttp3.Call) StringCallback(com.zhy.http.okhttp.callback.StringCallback) Request(okhttp3.Request) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) JSONException(org.json.JSONException) JsonResponse(com.stx.xhb.dmgameapp.utils.JsonResponse) JSONObject(org.json.JSONObject) ForumBean(com.stx.xhb.dmgameapp.entity.ForumBean) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with StringCallback

use of com.zhy.http.okhttp.callback.StringCallback in project GzuClassSchedule by mnnyang.

the class VersionUpdate method checkUpdate.

public void checkUpdate(final HttpCallback<Version> callback) {
    OkHttpUtils.get().url(Url.URL_CHECK_UPDATE_APP).build().execute(new StringCallback() {

        @Override
        public void onError(Call call, Exception e, int id) {
            e.printStackTrace();
            callback.onFail(e.getMessage());
        }

        @Override
        public void onResponse(String response, int id) {
            try {
                Gson gson = new Gson();
                Version version = gson.fromJson(response, Version.class);
                callback.onSuccess(version);
            } catch (Exception e) {
                e.printStackTrace();
                callback.onFail("parse error");
            }
        }
    });
}
Also used : Call(okhttp3.Call) Version(com.mnnyang.gzuclassschedule.data.bean.Version) StringCallback(com.zhy.http.okhttp.callback.StringCallback) Gson(com.google.gson.Gson) ActivityNotFoundException(android.content.ActivityNotFoundException)

Example 4 with StringCallback

use of com.zhy.http.okhttp.callback.StringCallback in project GzuClassSchedule by mnnyang.

the class HttpUtils method toImpt.

/**
 * @param xh
 * @param courseTime 格式: 2017-2018
 * @param term       学期num
 * @param callback
 */
public void toImpt(String xh, String courseTime, String term, final HttpCallback<String> callback) {
    LogUtil.d("toImpt", "xh" + xh + "c" + courseTime + "t" + term);
    OkHttpUtils.post().url(Url.URL_LOAD_COURSE + "?xh=" + xh + "&xm=%D1%EE%D3%D1%C1%D6&gnmkdm=N121603").addHeader("Referer", Url.URL_LOAD_COURSE + "?xh=" + xh + "&xm=%D1%EE%D3%D1%C1%D6&gnmkdm=N121603").addHeader("Connection", "keep-alive").addParams("__EVENTTARGET", "xnd").addParams("__EVENTARGUMENT", "").addParams("__VIEWSTATE", Url.VIEWSTATE_POST_CODE).addParams(Url.PARAM_XND, courseTime).addParams(Url.PARAM_XQD, term).build().execute(new StringCallback() {

        @Override
        public void onError(Call call, Exception e, int id) {
            e.printStackTrace();
            callback.onFail(ACCESS_ERR);
        }

        @Override
        public void onResponse(String response, int id) {
            Url.VIEWSTATE_POST_CODE = ParseCourse.parseViewStateCode(response);
            callback.onSuccess(response);
        }
    });
}
Also used : Call(okhttp3.Call) StringCallback(com.zhy.http.okhttp.callback.StringCallback)

Aggregations

StringCallback (com.zhy.http.okhttp.callback.StringCallback)4 Call (okhttp3.Call)3 Gson (com.google.gson.Gson)2 ActivityNotFoundException (android.content.ActivityNotFoundException)1 DialogInterface (android.content.DialogInterface)1 Version (com.mnnyang.gzuclassschedule.data.bean.Version)1 InternalVersionBean (com.mtm.mrecord.mvp.model.entity.InternalVersionBean)1 ForumBean (com.stx.xhb.dmgameapp.entity.ForumBean)1 JsonResponse (com.stx.xhb.dmgameapp.utils.JsonResponse)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Request (okhttp3.Request)1 JSONArray (org.json.JSONArray)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1