Search in sources :

Example 1 with TaskException

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

the class VideoService method getPicture.

public static void getPicture(VideoBean video) throws Exception {
    if (TextUtils.isEmpty(AppContext.getAccount().getCookie())) {
        throw new TaskException("123", "解析链接失败");
    }
    HttpConfig config = new HttpConfig();
    config.baseUrl = video.getShortUrl();
    config.cookie = AppContext.getAccount().getCookie();
    config.addHeader("Content-Type", "text/html;charset=utf-8");
    Setting action = new Setting();
    action.setType("");
    action.setValue("");
    action.setDescription("");
    String response = new DefHttpUtility().doGet(config, action, null, String.class);
    Document dom = Jsoup.parse(response);
    video.setIdStr(KeyGenerator.generateMD5(video.getShortUrl()));
    Elements divs = dom.select("img");
    if (divs != null && divs.size() > 0) {
        video.setImage(divs.get(0).attr("src"));
        video.setImage(video.getImage().replace("bmiddle", "small").replace("thumbnail", "small"));
    }
    if (TextUtils.isEmpty(video.getImage())) {
        String longUrl = video.getLongUrl();
        if (TextUtils.isEmpty(longUrl)) {
            UrlsBean urlsBean = SinaSDK.getInstance(AppContext.getAccount().getAccessToken()).urlShort2Long(video.getShortUrl());
            if (urlsBean.getUrls() != null && urlsBean.getUrls().size() > 0) {
                longUrl = urlsBean.getUrls().get(0).getUrl_long();
                longUrl.replace("bmiddle", "small").replace("thumbnail", "small");
            }
        }
        if (!TextUtils.isEmpty(longUrl)) {
            video.setImage(longUrl);
        }
    }
}
Also used : TaskException(org.aisen.android.network.task.TaskException) Setting(org.aisen.android.common.setting.Setting) HttpConfig(org.aisen.android.network.http.HttpConfig) SpannableString(android.text.SpannableString) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) UrlsBean(org.aisen.weibo.sina.sinasdk.bean.UrlsBean) DefHttpUtility(org.aisen.android.network.http.DefHttpUtility)

Example 2 with TaskException

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

the class SinaSDK method webConfig.

// 以下开始计划写网页版接口
private HttpConfig webConfig() throws TaskException {
    if (AppContext.getAccount() == null || AppContext.getAccount().getCookie() == null)
        throw new TaskException("", "网页授权已失效,请点击左侧私信菜单再次登录授权");
    HttpConfig config = configHttpConfig();
    config.baseUrl = WEB_BASE_URL;
    config.cookie = AppContext.getAccount().getCookie();
    config.addHeader("User-Agent", "Mozilla/5.0 (Linux; Android 5.1.1; SM801 Build/LMY47V) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/39.0.0.0 Mobile Safari/537.36");
    return config;
}
Also used : TaskException(org.aisen.android.network.task.TaskException) HttpConfig(org.aisen.android.network.http.HttpConfig)

Example 3 with TaskException

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

the class SinaSDK method searchsSuggest.

/**
 * 使用H5页面的接口拉取数据
 *
 * @param q
 * @param cookies
 * @return
 * @throws TaskException
 */
public String[] searchsSuggest(String q, String cookies) throws TaskException {
    // http://m.weibo.cn/searchs/suggest?count=10&q=ann
    Setting action = newSetting("searchsSuggest", "searchs/suggest", "获取搜索建议");
    action.getExtras().put(BASE_URL, newSettingExtra(BASE_URL, "http://m.weibo.cn/", ""));
    Params params = new Params();
    params.addParameter("q", q);
    params.addParameter("count", "5");
    HttpConfig config = configHttpConfig();
    config.cookie = cookies;
    config.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    config.addHeader("Referer", "http://m.weibo.cn/searchs");
    try {
        // ["ana",["anastasia","T-ANA小芹","anastasia 修容","广安门医院官方微博","anastasia 高光"]]
        String response = doPost(config, action, null, params, null, String.class);
        response = AisenUtils.convertUnicode(response);
        Logger.d("SinaSDK", response);
        if (response.toLowerCase().indexOf("<html>") != -1) {
            throw new TaskException("cookieinvalid", "网页版登录失效了");
        }
        // {"request":"/search/suggestions/all.php","error_code":"21405","error":"Operation timed out after 300 milliseconds with 0 bytes received url:http://i.api.weibo.com/users/show_batch.json"}
        if (response.indexOf("error_code") != -1 && response.indexOf("error") != -1) {
            JSONObject jsonRespone = JSONObject.parseObject(response);
            throw new TaskException(jsonRespone.getString("error_code"), jsonRespone.getString("error"));
        } else // {"ok":-100,"msg":"请先登录","url":"https://passport.weibo.cn/signin/welcome?entry=mweibo&r=http%3A%2F%2Fm.weibo.cn%2Fsearchs%2Fsuggest"}
        if (response.indexOf("ok") != -1 && response.indexOf("msg") != -1) {
            JSONObject jsonRespone = JSONObject.parseObject(response);
            throw new TaskException(jsonRespone.getString("ok"), jsonRespone.getString("msg"));
        }
        JSONArray jsonArray = JSON.parseArray(response);
        JSONArray resultArray = jsonArray.getJSONArray(1);
        String[] result = new String[resultArray.size()];
        for (int i = 0; i < resultArray.size(); i++) {
            result[i] = resultArray.getString(i);
        }
        return result;
    } catch (Exception e) {
        e.printStackTrace();
        if (e instanceof TaskException) {
            throw e;
        }
    }
    return new String[0];
}
Also used : TaskException(org.aisen.android.network.task.TaskException) JSONObject(com.alibaba.fastjson.JSONObject) Setting(org.aisen.android.common.setting.Setting) JSONArray(com.alibaba.fastjson.JSONArray) Params(org.aisen.android.network.http.Params) HttpConfig(org.aisen.android.network.http.HttpConfig) ParseException(java.text.ParseException) TaskException(org.aisen.android.network.task.TaskException)

Example 4 with TaskException

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

the class SinaSDK method searchsResultStatuss.

/**
 * H5接口搜索微博
 *
 * @param q
 * @param page
 * @param cookies
 * @return
 * @throws TaskException
 */
public ArrayList<StatusContent> searchsResultStatuss(String q, int page, String cookies) throws TaskException {
    ArrayList<StatusContent> resultUsers = new ArrayList<>();
    Setting action = newSetting("searchsResultUsers", "page/pageJson", "获取用户");
    action.getExtras().put(BASE_URL, newSettingExtra(BASE_URL, "http://m.weibo.cn/", ""));
    Params params = new Params();
    params.addParameter("containerid", "100103type%3D2%26q%3D" + q + "&page=" + page);
    try {
        String response = doGet(getHttpConfig(), action, params, String.class);
        JSONObject responseJSON = JSONObject.parseObject(response);
        int ok = responseJSON.getInteger("ok");
        if (ok == 1) {
            JSONArray cardsArray = responseJSON.getJSONArray("cards");
            for (int i = 0; i < cardsArray.size(); i++) {
                JSONObject cardGroupsObject = cardsArray.getJSONObject(i);
                JSONArray cardGroupArray = cardGroupsObject.getJSONArray("card_group");
                for (int j = 0; j < cardGroupArray.size(); j++) {
                    JSONObject cardGroup = cardGroupArray.getJSONObject(j);
                    JSONObject mblogObject = cardGroup.getJSONObject("mblog");
                    StatusContent content = JSON.parseObject(mblogObject.toJSONString(), StatusContent.class);
                    // 图片
                    if (mblogObject.containsKey("pics")) {
                        JSONArray picsArray = mblogObject.getJSONArray("pics");
                        if (picsArray != null && picsArray.size() > 0) {
                            PicUrls picUrls = new PicUrls();
                            picUrls.setThumbnail_pic(picsArray.getJSONObject(0).getString("url"));
                            content.setPic_urls(new PicUrls[] { picUrls });
                        }
                    }
                    // 把Html5文本转换一下
                    content.setText(Html.fromHtml(content.getText()).toString());
                    if (content.getRetweeted_status() != null) {
                        content.getRetweeted_status().setText(Html.fromHtml(content.getRetweeted_status().getText()).toString());
                    }
                    // 把时间转换一下
                    try {
                        Calendar calendar = Calendar.getInstance();
                        int year = calendar.get(Calendar.YEAR);
                        SimpleDateFormat format = new SimpleDateFormat("MM-dd HH:mm");
                        calendar.setTimeInMillis(format.parse(content.getCreated_at()).getTime());
                        calendar.set(Calendar.YEAR, year);
                        content.setCreated_at(calendar.getTimeInMillis() + "");
                    } catch (ParseException e) {
                        try {
                            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                            content.setCreated_at(format.parse(content.getCreated_at()).getTime() + "");
                        } catch (ParseException ewe) {
                        }
                    }
                    resultUsers.add(content);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        if (e instanceof TaskException) {
            throw (TaskException) e;
        }
    }
    return resultUsers;
}
Also used : StatusContent(org.aisen.weibo.sina.sinasdk.bean.StatusContent) Setting(org.aisen.android.common.setting.Setting) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) JSONArray(com.alibaba.fastjson.JSONArray) Params(org.aisen.android.network.http.Params) ParseException(java.text.ParseException) TaskException(org.aisen.android.network.task.TaskException) JSONObject(com.alibaba.fastjson.JSONObject) PicUrls(org.aisen.weibo.sina.sinasdk.bean.PicUrls) TaskException(org.aisen.android.network.task.TaskException) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat)

Example 5 with TaskException

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

the class SinaSDK method commentsHotShow.

/**
 * 热门评论
 *
 * @param statusId
 * @param page
 * @return
 * @throws TaskException
 */
public StatusComments commentsHotShow(String statusId, int page) throws TaskException {
    if (AppContext.getAccount() == null || AppContext.getAccount().getCookie() == null)
        throw new TaskException("", "网页授权已失效,请点击左侧私信菜单再次登录授权");
    HttpConfig config = configHttpConfig();
    config.baseUrl = "http://m.weibo.cn/single/rcList";
    config.cookie = AppContext.getAccount().getCookie();
    Params params = new Params();
    params.addParameter("id", statusId);
    params.addParameter("format", "cards");
    params.addParameter("type", "comment");
    params.addParameter("hot", "1");
    if (page > 1) {
        params.addParameter("page", String.valueOf(page));
    }
    Setting action = newSetting("", "", "");
    action.getExtras().put(HTTP_UTILITY, newSettingExtra(HTTP_UTILITY, CommentsHotHttpUtility.class.getName(), ""));
    return doGet(config, action, params, StatusComments.class);
}
Also used : 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)

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