Search in sources :

Example 11 with TaskException

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

the class TopicHotHttpUtility method parseResponse.

@Override
protected <T> T parseResponse(String resultStr, Class<T> responseCls) throws TaskException {
    try {
        AisenUtils.checkWebResult(resultStr);
        WebHotTopicssBean beans = new WebHotTopicssBean();
        beans.setList(new ArrayList<WebHotTopicsBean>());
        JSONObject result = JSONObject.parseObject(resultStr);
        if (result.containsKey("error")) {
            throw new TaskException("", result.getString("error"));
        }
        if (result.containsKey("ok")) {
            if (result.getInteger("ok") == 0 && result.containsKey("msg")) {
                throw new TaskException("", result.getString("msg"));
            }
            JSONObject cardlistInfo = result.getJSONObject("cardlistInfo");
            if (cardlistInfo.containsKey("since_id")) {
                beans.setSince_id(cardlistInfo.getString("since_id"));
            } else if (cardlistInfo.containsKey("page")) {
                beans.setPage(cardlistInfo.getInteger("page"));
            } else {
                beans.setEndPaging(true);
                return (T) beans;
            }
            JSONArray cards = result.getJSONArray("cards");
            if (cards.size() > 0) {
                JSONArray card_group = null;
                for (int i = 0; i < cards.size(); i++) {
                    if (cards.getJSONObject(i).containsKey("card_group")) {
                        card_group = cards.getJSONObject(i).getJSONArray("card_group");
                        break;
                    }
                }
                if (card_group != null) {
                    for (int i = 0; i < card_group.size(); i++) {
                        JSONObject group = card_group.getJSONObject(i);
                        WebHotTopicsBean bean = new WebHotTopicsBean();
                        bean.setCard_type(group.getInteger("card_type"));
                        // 普通样式
                        if (bean.getCard_type() == 8) {
                            bean.setPic(group.getString("pic"));
                            bean.setTitle_sub(group.getString("title_sub"));
                            bean.setCard_type_name(group.getString("card_type_name"));
                            bean.setDesc1(group.getString("desc1"));
                            bean.setDesc2(group.getString("desc2"));
                            bean.setOid(group.getJSONObject("actionlog").getString("oid"));
                            bean.setFid(group.getJSONObject("actionlog").getString("fid"));
                        } else // 多个图片、暂不支持
                        if (bean.getCard_type() == 3) {
                            continue;
                        } else {
                            continue;
                        }
                        beans.getList().add(bean);
                    }
                }
            }
            return (T) beans;
        } else {
            return (T) beans;
        }
    } catch (Exception e) {
        if (e instanceof TaskException)
            throw e;
        throw new TaskException(TaskException.TaskError.resultIllegal.toString());
    }
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) TaskException(org.aisen.android.network.task.TaskException) WebHotTopicssBean(org.aisen.weibo.sina.sinasdk.bean.WebHotTopicssBean) JSONArray(com.alibaba.fastjson.JSONArray) WebHotTopicsBean(org.aisen.weibo.sina.sinasdk.bean.WebHotTopicsBean) TaskException(org.aisen.android.network.task.TaskException)

Example 12 with TaskException

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

the class BizFragment method remindSetCount.

public void remindSetCount(final RemindType remindType) {
    // 测试通知功能时,不清零
    if (AppSettings.ignoreUnread()) {
        return;
    }
    final String uid = AppContext.getAccount().getUser().getIdstr();
    new WorkTask<RemindType, Void, SetCount>() {

        @Override
        public SetCount workInBackground(RemindType... params) throws TaskException {
            return SinaSDK.getInstance(AppContext.getAccount().getAccessToken()).remindSetCount(params[0].toString());
        }

        @Override
        protected void onSuccess(SetCount result) {
            super.onSuccess(result);
            if (getRealActivity() == null)
                return;
            UnreadCount count = AppContext.getAccount().getUnreadCount();
            if (count != null) {
                UnreadCountNotifier notifier = new UnreadCountNotifier(getRealActivity());
                if (remindType == RemindType.cmt) {
                    count.setCmt(0);
                    notifier.cancelNotification(Notifier.RemindUnreadComments);
                } else if (remindType == RemindType.follower) {
                    count.setFollower(0);
                    notifier.cancelNotification(Notifier.RemindUnreadForFollowers);
                } else if (remindType == RemindType.mention_cmt) {
                    count.setMention_cmt(0);
                    notifier.cancelNotification(Notifier.RemindUnreadForMentionComments);
                } else if (remindType == RemindType.mention_status) {
                    count.setMention_status(0);
                    notifier.cancelNotification(Notifier.RemindUnreadForMentionStatus);
                }
                // 更新DB
                SinaDB.getDB().insert(new Extra(uid, null), count);
                AppContext.getAccount().setUnreadCount(count);
                UnreadCountNotifier.mCount = count;
                // 发出广播更新状态
                UnreadService.sendUnreadBroadcast();
            }
        }
    }.execute(remindType);
}
Also used : SetCount(org.aisen.weibo.sina.sinasdk.bean.SetCount) UnreadCount(org.aisen.weibo.sina.sinasdk.bean.UnreadCount) TaskException(org.aisen.android.network.task.TaskException) Extra(org.aisen.android.component.orm.extra.Extra) UnreadCountNotifier(org.aisen.weibo.sina.service.notifier.UnreadCountNotifier)

Example 13 with TaskException

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

the class BizFragment method destoryFollower.

/* 结束查看用户好友列表 */
// XXX /*开始移除粉丝*/
public void destoryFollower(final WeiBoUser user, final OnDestoryFollowerCallback callback) {
    Token token = AppContext.getAccount().getAccessToken();
    if (AppContext.getAccount().getAdvancedToken() != null)
        token = AppContext.getAccount().getAdvancedToken();
    final Token trueToken = token;
    new MaterialDialog.Builder(getRealActivity()).title(R.string.title_destory_friend).content(R.string.biz_destory_follower).negativeText(R.string.cancel).positiveText(R.string.confirm).onPositive(new MaterialDialog.SingleButtonCallback() {

        @Override
        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
            new WorkTask<Void, Void, WeiBoUser>() {

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

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

                @Override
                protected void onFailure(TaskException exception) {
                    super.onFailure(exception);
                    showMessage(exception.getMessage());
                }

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

                @Override
                protected void onFinished() {
                    super.onFinished();
                    ViewUtils.dismissProgressDialog();
                }
            }.execute();
        }
    }).show();
}
Also used : MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) TaskException(org.aisen.android.network.task.TaskException) DialogAction(com.afollestad.materialdialogs.DialogAction) NonNull(android.support.annotation.NonNull) WeiBoUser(org.aisen.weibo.sina.sinasdk.bean.WeiBoUser) Token(org.aisen.weibo.sina.sinasdk.bean.Token)

Example 14 with TaskException

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

the class BizFragment method commentDestory.

// XXX /*删除评论*/
/* 删除评论 */
public void commentDestory(final StatusComment commnet, final OnCommentDestoryCallback callback) {
    final WeiBoUser user = AppContext.getAccount().getUser();
    new WorkTask<Void, Void, StatusComment>() {

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

        protected void onFinished() {
            super.onFinished();
            ViewUtils.dismissProgressDialog();
        }

        protected void onSuccess(StatusComment result) {
            super.onSuccess(result);
            if (getRealActivity() == null) {
                return;
            }
            if (callback != null)
                callback.onCommentDestory(commnet);
            showMessage(R.string.delete_success);
            // 删除成功后,DB同时也删除
            SinaDB.getTimelineDB().deleteById(new Extra(user.getIdstr(), null), StatusComment.class, result.getId());
        }

        protected void onFailure(TaskException exception) {
            super.onFailure(exception);
            if (getRealActivity() == null) {
                return;
            }
            if (!TextUtils.isEmpty(exception.getMessage()))
                showMessage(exception.getMessage());
            else
                showMessage(R.string.delete_faild);
        }

        @Override
        public StatusComment workInBackground(Void... params) throws TaskException {
            return SinaSDK.getInstance(AppContext.getAccount().getAccessToken()).commentsDestory(commnet.getId());
        }
    }.execute();
}
Also used : Extra(org.aisen.android.component.orm.extra.Extra) TaskException(org.aisen.android.network.task.TaskException) WeiBoUser(org.aisen.weibo.sina.sinasdk.bean.WeiBoUser) StatusComment(org.aisen.weibo.sina.sinasdk.bean.StatusComment)

Example 15 with TaskException

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

the class WebDownloader method downloadBitmap.

@Override
public byte[] downloadBitmap(Context context, String url, ImageConfig config) throws Exception {
    Logger.v(url);
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        DownloadProcess progress = config.getProgress();
        if (progress != null)
            progress.sendPrepareDownload(url);
        Request request = new Request.Builder().url(url).build();
        // httpGet.addHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:27.0) Gecko/20100101 Firefox/27.0");
        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 {
            // 图片大小
            int length = 0;
            try {
                String header = response.header("Content-Length");
                length = Integer.parseInt(header);
            } catch (Exception e) {
            }
            if (progress != null) {
                progress.sendLength(length);
            }
            InputStream in = response.body().byteStream();
            // 获取图片数据
            byte[] buffer = new byte[1024 * 8];
            int readLen = -1;
            int readBytes = 0;
            while ((readLen = in.read(buffer)) != -1) {
                readBytes += readLen;
                if (progress != null)
                    progress.sendProgress(readBytes);
                out.write(buffer, 0, readLen);
            }
            byte[] bs = out.toByteArray();
            // 如果图片没有下载完成,默认图片加载失败
            if (length != 0 && bs.length != length)
                return null;
            in.close();
            out.close();
            return bs;
        }
    } catch (Exception e) {
        e.printStackTrace();
        if (config.getProgress() != null)
            config.getProgress().sendException(e);
        throw new Exception(e.getCause());
    }
}
Also used : Response(com.squareup.okhttp.Response) TaskException(org.aisen.android.network.task.TaskException) InputStream(java.io.InputStream) Request(com.squareup.okhttp.Request) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TaskException(org.aisen.android.network.task.TaskException)

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