Search in sources :

Example 41 with TaskException

use of org.aisen.android.network.task.TaskException in project AisenWeiBo by wangdan.

the class BizFragment method createFriendship.

/* 结束取消对某用户的关注 */
// XXX /*添加关注*/
public void createFriendship(final WeiBoUser user, final OnCreateFriendshipCallback callback) {
    Token token = AppContext.getAccount().getAccessToken();
    if (AppContext.getAccount().getAdvancedToken() != null)
        token = AppContext.getAccount().getAdvancedToken();
    final Token trueToken = token;
    new WorkTask<Void, Void, WeiBoUser>() {

        @Override
        public WeiBoUser workInBackground(Void... params) throws TaskException {
            return SinaSDK.getInstance(trueToken).friendshipsCreate(user.getIdstr());
        }

        @Override
        protected void onPrepare() {
            super.onPrepare();
            ViewUtils.createProgressDialog(getRealActivity(), getRealString(R.string.biz_create_friendship), ThemeUtils.getThemeColor()).show();
        }

        @Override
        protected void onFailure(TaskException exception) {
            super.onFailure(exception);
            if (getRealActivity() == null) {
                return;
            }
            showMessage(exception.getMessage());
        }

        @Override
        protected void onSuccess(WeiBoUser result) {
            super.onSuccess(result);
            if (getRealActivity() == null) {
                return;
            }
            callback.onFriendshipCreated(result);
        }

        @Override
        protected void onFinished() {
            super.onFinished();
            if (getRealActivity() == null) {
                return;
            }
            ViewUtils.dismissProgressDialog();
        }
    }.execute();
}
Also used : TaskException(org.aisen.android.network.task.TaskException) WeiBoUser(org.aisen.weibo.sina.sinasdk.bean.WeiBoUser) Token(org.aisen.weibo.sina.sinasdk.bean.Token)

Example 42 with TaskException

use of org.aisen.android.network.task.TaskException in project AisenWeiBo by wangdan.

the class PictureSizeHttpUtility method doGet.

@Override
public <T> T doGet(HttpConfig config, Setting action, Params urlParams, Class<T> responseCls) throws TaskException {
    if (GlobalContext.getInstance() == null || SystemUtils.getNetworkType(GlobalContext.getInstance()) == SystemUtils.NetWorkType.none)
        return null;
    String url = urlParams.getParameter("path");
    PictureSize size = new PictureSize();
    size.setUrl(url);
    Request request = new Request.Builder().url(url).build();
    try {
        Response response = GlobalContext.getOkHttpClient().newCall(request).execute();
        if (!(response.code() == HttpURLConnection.HTTP_OK || response.code() == HttpURLConnection.HTTP_PARTIAL)) {
            throw new TaskException(String.valueOf(TaskException.TaskError.failIOError));
        } else {
            // 图片大小
            String header = response.header("Content-Length");
            int length = Integer.parseInt(header);
            size.setSize(length);
            SinaDB.getDB().insert(null, size);
            Logger.d(TAG, String.format("图片大小 %s", String.valueOf(size.getSize())));
        }
    } catch (Exception e) {
        throw new TaskException(String.valueOf(TaskException.TaskError.failIOError));
    }
    return (T) size;
}
Also used : Response(com.squareup.okhttp.Response) PictureSize(org.aisen.weibo.sina.support.bean.PictureSize) TaskException(org.aisen.android.network.task.TaskException) Request(com.squareup.okhttp.Request) TaskException(org.aisen.android.network.task.TaskException)

Example 43 with TaskException

use of org.aisen.android.network.task.TaskException in project AisenWeiBo by wangdan.

the class SinaErrorMsgUtil method checkResponse.

@Override
public void checkResponse(String response) throws TaskException {
    Logger.w(response);
    if (!TextUtils.isEmpty(response)) {
        if (response.indexOf("that page doesn’t exist!") != -1)
            throw new TaskException("", "渣浪TMD的神经病,请稍后重试...");
    }
    String code = null;
    String msg = null;
    if (!TextUtils.isEmpty(response)) {
        JSONObject jsonMsg;
        try {
            jsonMsg = new JSONObject(response);
            if (jsonMsg.has("error")) {
                if (jsonMsg.has("error_code")) {
                    msg = jsonMsg.getString("error");
                    if ("invalid_access_token".equals(msg)) {
                        code = "21327";
                    } else {
                        code = jsonMsg.getString("error_code");
                    }
                    if (errorMap.containsKey(code))
                        msg = errorMap.get(code);
                }
            }
        } catch (Exception e) {
        }
    }
    if (!TextUtils.isEmpty(msg))
        throw new TaskException(code, msg);
    throw new TaskException(code);
}
Also used : TaskException(org.aisen.android.network.task.TaskException) JSONObject(org.json.JSONObject) TaskException(org.aisen.android.network.task.TaskException)

Example 44 with TaskException

use of org.aisen.android.network.task.TaskException in project AisenWeiBo by wangdan.

the class TimelineHotHttpUtility method parseResponse.

@Override
protected <T> T parseResponse(String resultStr, Class<T> responseCls) throws TaskException {
    try {
        StatusHots statusHots = super.parseResponse(resultStr, StatusHots.class);
        if (statusHots.getCards().size() == 0) {
            StatusContents result = new StatusContents();
            result.setStatuses(new ArrayList<StatusContent>());
            result.setEndPaging(true);
            return (T) result;
        }
        String[] ids = new String[statusHots.getCards().size()];
        for (int i = 0; i < statusHots.getCards().size(); i++) {
            ids[i] = statusHots.getCards().get(i).getMblog().getId() + "";
        }
        return (T) SinaSDK.getInstance(AppContext.getAccount().getAccessToken()).statusShowBatch(ids);
    } catch (Exception e) {
        throw new TaskException(TaskException.TaskError.resultIllegal.toString());
    }
}
Also used : StatusContent(org.aisen.weibo.sina.sinasdk.bean.StatusContent) TaskException(org.aisen.android.network.task.TaskException) StatusHots(org.aisen.weibo.sina.sinasdk.bean.StatusHots) StatusContents(org.aisen.weibo.sina.sinasdk.bean.StatusContents) TaskException(org.aisen.android.network.task.TaskException)

Example 45 with TaskException

use of org.aisen.android.network.task.TaskException in project AisenWeiBo by wangdan.

the class TimelineHotTopicsHttpUtility method parseResponse.

@Override
protected <T> T parseResponse(String resultStr, Class<T> responseCls) throws TaskException {
    try {
        AisenUtils.checkWebResult(resultStr);
        JSONObject result = JSONObject.parseObject(resultStr);
        if (result.containsKey("error")) {
            throw new TaskException(result.getString("error_code"), result.getString("error"));
        }
        if (result.containsKey("ok")) {
            if (result.getInteger("ok") == 0 && result.containsKey("msg")) {
                throw new TaskException("", result.getString("msg"));
            }
            JSONArray cards = result.getJSONArray("cards");
            StatusContents beans;
            if (cards.size() > 0) {
                JSONArray card_group = null;
                for (int i = 0; i < cards.size(); i++) {
                    JSONObject card = cards.getJSONObject(i);
                    if (card.containsKey("card_group")) {
                        // }
                        if (card.containsKey("_cur_filter")) {
                            card_group = cards.getJSONObject(i).getJSONArray("card_group");
                            break;
                        }
                    }
                }
                if (card_group.size() == 0) {
                    beans = new StatusContents();
                    beans.setStatuses(new ArrayList<StatusContent>());
                    beans.setEndPaging(true);
                    return (T) beans;
                }
                List<String> idList = new ArrayList<>();
                for (int i = 0; i < card_group.size(); i++) {
                    JSONObject group = card_group.getJSONObject(i);
                    if (group.containsKey("mblog")) {
                        idList.add(group.getJSONObject("mblog").getString("id"));
                    }
                }
                String[] ids = new String[idList.size()];
                for (int i = 0; i < idList.size(); i++) {
                    ids[i] = idList.get(i);
                }
                beans = SinaSDK.getInstance(AppContext.getAccount().getAccessToken()).statusShowBatch(ids);
                ;
            } else {
                beans = new StatusContents();
                beans.setStatuses(new ArrayList<StatusContent>());
            }
            if (result.containsKey("cardlistInfo")) {
                JSONObject cardlistInfo = result.getJSONObject("cardlistInfo");
                beans.setSince_id(cardlistInfo.getString("since_id"));
                // 是否加载完了
                int total = cardlistInfo.getInteger("total");
                beans.setEndPaging(beans.getStatuses().size() == total);
            } else if (result.containsKey("pageInfo")) {
                JSONObject cardlistInfo = result.getJSONObject("pageInfo");
                beans.setSince_id(cardlistInfo.getString("since_id"));
            }
            return (T) beans;
        } else {
            throw new TaskException("", "未知错误,请稍后再试");
        }
    } catch (Exception e) {
        if (e instanceof TaskException) {
            throw e;
        } else if (!TextUtils.isEmpty(resultStr) && resultStr.indexOf("你要找的页面") != -1) {
            throw new TaskException("", "啊哦,你要找的页面不见啦!");
        }
        throw new TaskException(TaskException.TaskError.resultIllegal.toString());
    }
}
Also used : StatusContent(org.aisen.weibo.sina.sinasdk.bean.StatusContent) JSONArray(com.alibaba.fastjson.JSONArray) ArrayList(java.util.ArrayList) TaskException(org.aisen.android.network.task.TaskException) JSONObject(com.alibaba.fastjson.JSONObject) TaskException(org.aisen.android.network.task.TaskException) StatusContents(org.aisen.weibo.sina.sinasdk.bean.StatusContents)

Aggregations

TaskException (org.aisen.android.network.task.TaskException)45 Setting (org.aisen.android.common.setting.Setting)11 Params (org.aisen.android.network.http.Params)10 File (java.io.File)9 HttpConfig (org.aisen.android.network.http.HttpConfig)8 WeiBoUser (org.aisen.weibo.sina.sinasdk.bean.WeiBoUser)8 MaterialDialog (com.afollestad.materialdialogs.MaterialDialog)7 DialogAction (com.afollestad.materialdialogs.DialogAction)6 JSONArray (com.alibaba.fastjson.JSONArray)6 JSONObject (com.alibaba.fastjson.JSONObject)6 ParseException (java.text.ParseException)6 Bitmap (android.graphics.Bitmap)5 ImageView (android.widget.ImageView)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 IOException (java.io.IOException)4 View (android.view.View)3 Request (com.squareup.okhttp.Request)3 Response (com.squareup.okhttp.Response)3 InputStream (java.io.InputStream)3 ArrayList (java.util.ArrayList)3