Esempio n. 1
1
  public UserListBean getUserList() throws WeiboException {

    String url = WeiBoURLs.USERS_SEARCH;

    Map<String, String> map = new HashMap<String, String>();
    map.put("access_token", access_token);
    map.put("count", count);
    map.put("page", page);
    map.put("q", q);

    String jsonData = null;

    jsonData = HttpUtility.getInstance().executeNormalTask(HttpMethod.Get, url, map);

    Gson gson = new Gson();

    UserListBean value = null;
    try {
      value = gson.fromJson(jsonData, UserListBean.class);
    } catch (JsonSyntaxException e) {

      AppLoggerUtils.e(e.getMessage());
    }

    return value;
  }
Esempio n. 2
0
  public List<String> getInfo() throws WeiboException {

    String url = WeiBoURLs.GROUP_MEMBER_LIST;

    Map<String, String> map = new HashMap<String, String>();
    map.put("access_token", access_token);
    map.put("uids", uids);

    String jsonData = HttpUtility.getInstance().executeNormalTask(HttpMethod.Get, url, map);

    Gson gson = new Gson();

    List<GroupUser> value = null;
    try {
      value = gson.fromJson(jsonData, new TypeToken<List<GroupUser>>() {}.getType());
    } catch (JsonSyntaxException e) {
      AppLoggerUtils.e(e.getMessage());
    }

    if (value != null && value.size() > 0) {
      GroupUser user = value.get(0);
      List<String> ids = new ArrayList<String>();
      for (GroupBean b : user.lists) {
        ids.add(b.getIdstr());
      }
      return ids;
    }

    return null;
  }
Esempio n. 3
0
  public String doGet(String urlStr, Map<String, String> param) throws WeiboException {
    BeeboApplication globalContext = BeeboApplication.getInstance();
    String errorStr = globalContext.getString(R.string.timeout);
    globalContext = null;
    InputStream is = null;
    try {

      StringBuilder urlBuilder = new StringBuilder(urlStr);
      urlBuilder.append("?").append(Utility.encodeUrl(param));
      URL url = new URL(urlBuilder.toString());
      AppLoggerUtils.d("get request" + url);
      Proxy proxy = getProxy();
      HttpURLConnection urlConnection;
      if (proxy != null) {
        urlConnection = (HttpURLConnection) url.openConnection(proxy);
      } else {
        urlConnection = (HttpURLConnection) url.openConnection();
      }

      urlConnection.setRequestMethod("GET");
      urlConnection.setDoOutput(false);
      urlConnection.setConnectTimeout(CONNECT_TIMEOUT);
      urlConnection.setReadTimeout(READ_TIMEOUT);
      urlConnection.setRequestProperty("Connection", "Keep-Alive");
      urlConnection.setRequestProperty("Charset", "UTF-8");
      urlConnection.setRequestProperty("Accept-Encoding", "gzip, deflate");

      urlConnection.connect();

      return handleResponse(urlConnection);
    } catch (IOException e) {
      e.printStackTrace();
      throw new WeiboException(errorStr, e);
    }
  }
Esempio n. 4
0
  private String handleError(HttpURLConnection urlConnection) throws WeiboException {

    String result = readError(urlConnection);
    String err = null;
    int errCode = 0;
    try {
      AppLoggerUtils.e("error=" + result);
      JSONObject json = new JSONObject(result);
      err = json.optString("error_description", "");
      if (TextUtils.isEmpty(err)) {
        err = json.getString("error");
      }
      errCode = json.getInt("error_code");
      WeiboException exception = new WeiboException();
      exception.setError_code(errCode);
      exception.setOriError(err);

      if (errCode == ErrorCode.EXPIRED_TOKEN || errCode == ErrorCode.INVALID_TOKEN) {
        Utility.showExpiredTokenDialogOrNotification();
      }

      throw exception;

    } catch (JSONException e) {
      e.printStackTrace();
    }

    return result;
  }
Esempio n. 5
0
  public SearchStatusListBean getStatusList() throws WeiboException {

    String url = WeiBoURLs.STATUSES_SEARCH;

    Map<String, String> map = new HashMap<String, String>();
    map.put("access_token", access_token);
    map.put("count", count);
    map.put("page", page);
    map.put("q", q);

    String jsonData = null;

    jsonData = HttpUtility.getInstance().executeNormalTask(HttpMethod.Get, url, map);

    Gson gson = new Gson();

    SearchStatusListBean value = null;
    try {
      value = gson.fromJson(jsonData, SearchStatusListBean.class);
      List<MessageBean> list = value.getItemList();

      Iterator<MessageBean> iterator = list.iterator();
      while (iterator.hasNext()) {
        MessageBean msg = iterator.next();
        // message is deleted by sina
        if (msg.getUser() == null) {
          iterator.remove();
        } else {
          msg.getListViewSpannableString();
          TimeUtility.dealMills(msg);
        }
      }
    } catch (JsonSyntaxException e) {

      AppLoggerUtils.e(e.getMessage());
    }

    return value;
  }
Esempio n. 6
0
  private String readError(HttpURLConnection urlConnection) throws WeiboException {
    InputStream is = null;
    BufferedReader buffer = null;
    BeeboApplication globalContext = BeeboApplication.getInstance();
    String errorStr = globalContext.getString(R.string.timeout);

    try {
      is = urlConnection.getErrorStream();

      if (is == null) {
        errorStr = globalContext.getString(R.string.unknown_sina_network_error);
        throw new WeiboException(errorStr);
      }

      String content_encode = urlConnection.getContentEncoding();

      if (!TextUtils.isEmpty(content_encode) && content_encode.equals("gzip")) {
        is = new GZIPInputStream(is);
      }

      buffer = new BufferedReader(new InputStreamReader(is));
      StringBuilder strBuilder = new StringBuilder();
      String line;
      while ((line = buffer.readLine()) != null) {
        strBuilder.append(line);
      }
      AppLoggerUtils.d("error result=" + strBuilder.toString());
      return strBuilder.toString();
    } catch (IOException e) {
      e.printStackTrace();
      throw new WeiboException(errorStr, e);
    } finally {
      Utility.closeSilently(is);
      Utility.closeSilently(buffer);
      urlConnection.disconnect();
      globalContext = null;
    }
  }
Esempio n. 7
0
  public boolean doGetSaveFile(
      String urlStr, String path, FileDownloaderHttpHelper.DownloadListener downloadListener) {

    File file = FileManager.createNewFileInSDCard(path);
    if (file == null) {
      return false;
    }

    boolean result = false;

    BufferedOutputStream out = null;
    InputStream in = null;
    HttpURLConnection urlConnection = null;
    try {

      URL url = new URL(urlStr);
      AppLoggerUtils.d("download request=" + urlStr);
      Proxy proxy = getProxy();
      if (proxy != null) {
        urlConnection = (HttpURLConnection) url.openConnection(proxy);
      } else {
        urlConnection = (HttpURLConnection) url.openConnection();
      }

      urlConnection.setRequestMethod("GET");
      urlConnection.setDoOutput(false);
      urlConnection.setConnectTimeout(DOWNLOAD_CONNECT_TIMEOUT);
      urlConnection.setReadTimeout(DOWNLOAD_READ_TIMEOUT);
      urlConnection.setRequestProperty("Connection", "Keep-Alive");
      urlConnection.setRequestProperty("Charset", "UTF-8");
      urlConnection.setRequestProperty("Accept-Encoding", "gzip, deflate");

      urlConnection.connect();

      int status = urlConnection.getResponseCode();

      if (status != HttpURLConnection.HTTP_OK) {
        return false;
      }

      int bytetotal = (int) urlConnection.getContentLength();
      int bytesum = 0;
      int byteread = 0;
      out = new BufferedOutputStream(new FileOutputStream(file));

      InputStream is = urlConnection.getInputStream();
      String content_encode = urlConnection.getContentEncoding();
      if (!TextUtils.isEmpty(content_encode) && content_encode.equals("gzip")) {
        is = new GZIPInputStream(is);
      }
      in = new BufferedInputStream(is);

      final Thread thread = Thread.currentThread();
      byte[] buffer = new byte[1444];
      while ((byteread = in.read(buffer)) != -1) {
        if (thread.isInterrupted()) {
          if (((float) bytesum / (float) bytetotal) < 0.8f) {
            file.delete();
            throw new InterruptedIOException();
          }
        }

        bytesum += byteread;
        out.write(buffer, 0, byteread);
        if (downloadListener != null && bytetotal > 0) {
          downloadListener.pushProgress(bytesum, bytetotal);
        }
      }
      if (downloadListener != null) {
        downloadListener.completed();
      }
      AppLoggerUtils.v("download request= " + urlStr + " download finished");
      result = true;

    } catch (IOException e) {
      e.printStackTrace();
      AppLoggerUtils.v("download request= " + urlStr + " download failed");
    } finally {
      Utility.closeSilently(in);
      Utility.closeSilently(out);
      if (urlConnection != null) {
        urlConnection.disconnect();
      }
    }

    return result && ImageUtility.isThisBitmapCanRead(path);
  }