Search in sources :

Example 6 with TaskException

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

the class SinaSDK method webStatusLike.

/**
 * 微博点赞
 *
 * @param statusId
 * @param like
 * @param cookie
 * @return
 * @throws TaskException
 */
public LikeResultBean webStatusLike(String statusId, boolean like, String cookie) throws TaskException {
    Params params = new Params();
    params.addParameter("id", statusId);
    if (like)
        params.addParameter("attitude", "heart");
    Setting action = newSetting("webStatusLike", like ? "attitudesDeal/add" : "attitudesDeal/delete", "微博点赞");
    HttpConfig config = webConfig();
    config.addHeader("Referer", config.baseUrl);
    config.addHeader("Content-Type", "application/x-www-form-urlencoded");
    config.addHeader("Host", "m.weibo.cn");
    config.addHeader("Accept-Language", "zh-CN,en-US;q=0.8");
    config.addHeader("Accept", "application/json, text/plain, */*");
    String body = doPost(config, action, null, params, null, String.class);
    if (!TextUtils.isEmpty(body)) {
        Logger.d(TAG, body);
        if (body.indexOf("http://passport.weibo.cn/sso/crossdomain") != -1)
            throw new TaskException("-100", "未登录");
        else if (body.indexOf("<html") != -1)
            throw new TaskException("-100", "未登录");
        LikeResultBean likeBean = JSON.parseObject(body, LikeResultBean.class);
        if (likeBean.getOk() == 1) {
            return likeBean;
        } else if (likeBean.getOk() == -100) {
            throw new TaskException("-100", "未登录");
        } else {
            throw new TaskException("", likeBean.getMsg());
        }
    }
    throw new TaskException(TaskException.TaskError.timeout.toString());
}
Also used : LikeResultBean(org.aisen.weibo.sina.support.bean.LikeResultBean) TaskException(org.aisen.android.network.task.TaskException) Setting(org.aisen.android.common.setting.Setting) Params(org.aisen.android.network.http.Params) HttpConfig(org.aisen.android.network.http.HttpConfig)

Example 7 with TaskException

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

the class SDK method getWallpaper.

/**
 * 获取壁纸列表
 *
 * @param page
 * @return
 * @throws TaskException
 */
public WallpaperBeans getWallpaper(int page) throws TaskException {
    Setting action = newSetting("getWallpaper", "wallpaper/newestorhot/content", "获取最新壁纸列表");
    action.getExtras().put(BASE_URL, newSettingExtra(BASE_URL, "http://apps.tclclouds.com/api/", ""));
    Params params = new Params();
    // 1:最新;2:最热)
    params.addParameter("flag", "2");
    params.addParameter("page", String.valueOf(page));
    params.addParameter("per_page", "30");
    params.addParameter("encoder", "debug");
    // 配置缓存器
    action.getExtras().put(CACHE_UTILITY, newSettingExtra(CACHE_UTILITY, WallpaperCacheUtility.class.getName(), ""));
    WallpaperBeans beans = doGet(action, params, WallpaperBeans.class);
    if (beans.getItem() == null || beans.getItem().getWallpaperList() == null) {
        throw new TaskException(TaskException.TaskError.resultIllegal.toString());
    }
    return beans;
}
Also used : TaskException(org.aisen.android.network.task.TaskException) WallpaperBeans(org.aisen.weibo.sina.support.bean.WallpaperBeans) Setting(org.aisen.android.common.setting.Setting) Params(org.aisen.android.network.http.Params)

Example 8 with TaskException

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

the class EmotionsDB method checkEmotions.

public static void checkEmotions() {
    Cursor cursor = null;
    // 检查表是否存在
    boolean tableExist = false;
    try {
        String sql = "SELECT COUNT(*) AS c FROM sqlite_master WHERE type ='table' AND name ='" + EmotionTable.table + "' ";
        cursor = emotionsDb.rawQuery(sql, null);
        if (cursor != null && cursor.moveToNext()) {
            int count = cursor.getInt(0);
            if (count > 0)
                tableExist = true;
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (cursor != null)
            cursor.close();
        cursor = null;
    }
    // 表情表不存在,创建表情表
    if (!tableExist) {
        Logger.v(TAG, "create emotions table");
        String sql = String.format("create table %s ( %s INTEGER PRIMARY KEY AUTOINCREMENT, %s TEXT , %s TEXT, %s BOLB)", EmotionTable.table, EmotionTable.id, EmotionTable.key, EmotionTable.file, EmotionTable.value);
        emotionsDb.execSQL(sql);
    } else {
        Logger.v(TAG, "emotions table exist");
    }
    boolean insertEmotions = true;
    // 表情不存在或者不全,插入表情
    try {
        cursor = emotionsDb.rawQuery(" select count(*) as c from " + EmotionTable.table, null);
        if (cursor != null && cursor.moveToFirst()) {
            int count = cursor.getInt(0);
            if (count == 159)
                insertEmotions = false;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    // 向数据库插入表情
    if (insertEmotions) {
        Logger.v(TAG, "insert emotions");
        new WorkTask<Void, Void, Void>() {

            @Override
            public Void workInBackground(Void... params) throws TaskException {
                InputStream in;
                try {
                    in = GlobalContext.getInstance().getAssets().open("emotions.properties");
                    Properties properties = new Properties();
                    properties.load(new InputStreamReader(in, "utf-8"));
                    Set<Object> keySet = properties.keySet();
                    // 开启事务
                    emotionsDb.beginTransaction();
                    emotionsDb.execSQL(String.format("delete from %s", EmotionTable.table));
                    for (Object key : keySet) {
                        String value = properties.getProperty(key.toString());
                        Logger.w(TAG, String.format("emotion's key(%s), value(%s)", key, value));
                        ContentValues values = new ContentValues();
                        values.put(EmotionTable.key, key.toString());
                        byte[] emotion = FileUtils.readStreamToBytes(GlobalContext.getInstance().getAssets().open(value));
                        values.put(EmotionTable.value, emotion);
                        values.put(EmotionTable.file, value);
                        emotionsDb.insert(EmotionTable.table, EmotionTable.id, values);
                    }
                    // 结束事务
                    emotionsDb.setTransactionSuccessful();
                    emotionsDb.endTransaction();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }
        }.execute();
    } else {
        Logger.v(TAG, "emotions exist");
    }
}
Also used : ContentValues(android.content.ContentValues) Set(java.util.Set) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Cursor(android.database.Cursor) Properties(java.util.Properties) TaskException(org.aisen.android.network.task.TaskException) TaskException(org.aisen.android.network.task.TaskException)

Example 9 with TaskException

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

the class HttpsUtility method parseResponse.

@Override
protected <T> T parseResponse(String resultStr, Class<T> responseCls) throws TaskException {
    T result = super.parseResponse(resultStr, responseCls);
    if (result instanceof OfflineService.OfflineLength) {
        OfflineService.OfflineLength iLength = (OfflineService.OfflineLength) result;
        iLength.setLength(resultStr.length());
    }
    if (result instanceof BaseSinaBean) {
        BaseSinaBean sinaBean = (BaseSinaBean) result;
        if (sinaBean.getError_code() > 0 && !TextUtils.isEmpty(sinaBean.getError())) {
            throw new TaskException(String.valueOf(sinaBean.getError_code()), sinaBean.getError());
        }
    }
    return result;
}
Also used : OfflineService(org.aisen.weibo.sina.service.OfflineService) TaskException(org.aisen.android.network.task.TaskException) BaseSinaBean(org.aisen.weibo.sina.sinasdk.bean.BaseSinaBean)

Example 10 with TaskException

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

the class CommentsHotHttpUtility method parseResponse.

@Override
protected <T> T parseResponse(String resultStr, Class<T> responseCls) throws TaskException {
    try {
        JSONArray jsonArray = JSONArray.parseArray(resultStr);
        StatusComments comments = new StatusComments();
        comments.setComments(new ArrayList<StatusComment>());
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            if (jsonObject.containsKey("card_group")) {
                JSONArray card_group = jsonObject.getJSONArray("card_group");
                for (int j = 0; j < card_group.size(); j++) {
                    try {
                        JSONObject card = card_group.getJSONObject(j);
                        StatusComment comment = new StatusComment();
                        comment.setId(card.getString("id"));
                        comment.setText(Html.fromHtml(card.getString("text")).toString());
                        comment.setCreated_at(card.getString("created_at"));
                        comment.setSource(card.getString("source"));
                        comment.setLikedCount(card.getLong("like_counts"));
                        comment.setLiked(card.getBoolean("liked"));
                        JSONObject user = card.getJSONObject("user");
                        comment.setUser(new WeiBoUser());
                        comment.getUser().setInfoAll(false);
                        comment.getUser().setId(user.getString("id"));
                        comment.getUser().setScreen_name(user.getString("screen_name"));
                        comment.getUser().setProfile_image_url(user.getString("profile_image_url"));
                        comments.getComments().add(comment);
                    } catch (Exception e) {
                        Logger.printExc(CommentsHotHttpUtility.class, e);
                    }
                }
            }
        }
        return (T) comments;
    } catch (Exception e) {
        throw new TaskException(TaskException.TaskError.resultIllegal.toString());
    }
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) TaskException(org.aisen.android.network.task.TaskException) JSONArray(com.alibaba.fastjson.JSONArray) StatusComment(org.aisen.weibo.sina.sinasdk.bean.StatusComment) WeiBoUser(org.aisen.weibo.sina.sinasdk.bean.WeiBoUser) StatusComments(org.aisen.weibo.sina.sinasdk.bean.StatusComments) 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