示例#1
0
  /**
   * 发送微博
   *
   * @param token
   * @param editText
   * @return
   */
  public static Boolean sendWeibo(Oauth2AccessToken token, EditText editText, byte[] imgBuffer) {
    HttpPost postMethod;

    // 组织post参数:此处只实现最简单的发送消息,只填两个参数,其他见http://open.weibo.com/wiki/2/statuses/friends_timeline
    HttpClient httpClient = new DefaultHttpClient();
    List<BasicNameValuePair> params = new LinkedList<BasicNameValuePair>();
    String messageString = editText.getText().toString();
    params.add(new BasicNameValuePair("status", messageString));
    params.add(new BasicNameValuePair("access_token", token.getToken()));
    // 判断是否有图片,选择不同的接口
    if (null != imgBuffer) {
      Log.w(TAG, "Image file include!");
      params.add(new BasicNameValuePair("pic", imgBuffer.toString()));
      postMethod = new HttpPost(Contants.STATUESES_UPDATA_WITH_IMG_URL);
    } else {
      Log.w(TAG, "No images select!");
      postMethod = new HttpPost(Contants.STATUESES_UPDATA_URL);
    }

    try {
      postMethod.setEntity(new UrlEncodedFormEntity(params, "utf-8"));
      HttpResponse httpResponse = httpClient.execute(postMethod);

      // 将返回结果转为字符串,通过文档可知返回结果为json字符串,结构请参考文档
      String resultStr = EntityUtils.toString(httpResponse.getEntity());
      Log.w(TAG, resultStr);

      // 从json字符串中建立JSONObject
      JSONObject resultJson = new JSONObject(resultStr);

      // 如果发送微博失败的话,返回字段中有"error"字段,通过判断是否存在该字段即可知道是否发送成功
      if (resultJson.has("error")) {
        return false;
      } else {
        return true;
      }

    } catch (UnsupportedEncodingException e) {
      Log.w(TAG, e.getLocalizedMessage());
    } catch (ClientProtocolException e) {
      Log.w(TAG, e.getLocalizedMessage());
    } catch (IOException e) {
      Log.w(TAG, e.getLocalizedMessage());
    } catch (ParseException e) {
      Log.w(TAG, e.getLocalizedMessage());
    } catch (JSONException e) {
      Log.w(TAG, e.getLocalizedMessage());
    }

    return false;
  }
示例#2
0
  public static long getCurrentUserId(Oauth2AccessToken token) {
    JSONObject resultJson = null;
    HttpResponse httpResponse;
    HttpClient httpClient = new DefaultHttpClient();

    // 传入get方法的请求地址和参数
    HttpGet getMethod =
        new HttpGet(Contants.GET_CURRENT_USER_INFO_URL + "?" + "access_token=" + token.getToken());
    try {
      // execute返回一个响应对象
      httpResponse = httpClient.execute(getMethod);
      // 响应的内容对象
      // 读取内容,用inputstream读取
      String resultStr = EntityUtils.toString(httpResponse.getEntity());
      resultJson = new JSONObject(resultStr);

      // 如果发送微博失败的话,返回字段中有"error"字段,通过判断是否存在该字段即可知道是否发送成功
      if (resultJson.has("error")) {
        Log.w(TAG, "获取用户信息失败");
      }
      // Log.i(TAG, resultJson.toString());

    } catch (UnsupportedEncodingException e) {
      Log.w(TAG, e.getLocalizedMessage());
    } catch (ClientProtocolException e) {
      Log.w(TAG, e.getLocalizedMessage());
    } catch (IOException e) {
      Log.w(TAG, e.getLocalizedMessage());
    } catch (ParseException e) {
      Log.w(TAG, e.getLocalizedMessage());
    } catch (JSONException e) {
      Log.w(TAG, e.getLocalizedMessage());
    }
    try {
      return resultJson.getLong("uid");
    } catch (JSONException e) {
      e.printStackTrace();
    }
    return 0;
  }
示例#3
0
  /**
   * 使用psot方法发送http请求
   *
   * @param context
   * @param params
   * @param baseUrl
   * @return
   */
  public static JSONObject doPost(
      Context context, List<BasicNameValuePair> params, String baseUrl) {
    JSONObject resultJson = null;
    HttpClient httpClient = new DefaultHttpClient();

    // 传入post方法的请求地址,即发送微博的api接口
    HttpPost postMethod = new HttpPost(baseUrl);
    try {
      postMethod.setEntity(new UrlEncodedFormEntity(params, "utf-8"));
      HttpResponse httpResponse = httpClient.execute(postMethod);

      // 将返回结果转为字符串,通过文档可知返回结果为json字符串,结构请参考文档
      String resultStr = EntityUtils.toString(httpResponse.getEntity());
      Log.w(TAG, resultStr);

      // 从json字符串中建立JSONObject
      resultJson = new JSONObject(resultStr);

      // 如果发送微博失败的话,返回字段中有"error"字段,通过判断是否存在该字段即可知道是否发送成功
      if (resultJson.has("error")) {
        Log.w(TAG, "post success");
      } else {
        Log.w(TAG, "post fali");
      }

    } catch (UnsupportedEncodingException e) {
      Log.w(TAG, e.getLocalizedMessage());
    } catch (ClientProtocolException e) {
      Log.w(TAG, e.getLocalizedMessage());
    } catch (IOException e) {
      Log.w(TAG, e.getLocalizedMessage());
    } catch (ParseException e) {
      Log.w(TAG, e.getLocalizedMessage());
    } catch (JSONException e) {
      Log.w(TAG, e.getLocalizedMessage());
    }
    return resultJson;
  }
示例#4
0
  /**
   * 获取所有新消息数,UID为当前登陆用户
   *
   * @return
   */
  public static NewMsg getNewMessageCounts(Oauth2AccessToken token, Long currentUID) {
    NewMsg newMsg = new NewMsg();
    JSONObject resultJson = null;
    HttpResponse httpResponse;
    HttpClient httpClient = new DefaultHttpClient();

    // 传入get方法的请求地址和参数
    HttpGet getMethod =
        new HttpGet(
            Contants.GET_MESSAGE_COUNT_URL
                + "?"
                + "access_token="
                + token.getToken()
                + "&true="
                + currentUID);
    try {
      // execute返回一个响应对象
      httpResponse = httpClient.execute(getMethod);
      // 响应的内容对象
      // 读取内容,用inputstream读取
      String resultStr = EntityUtils.toString(httpResponse.getEntity());
      resultJson = new JSONObject(resultStr);
      // 如果发送微博失败的话,返回字段中有"error"字段,通过判断是否存在该字段即可知道是否发送成功
      if (resultJson.has("error")) {
        Log.w(TAG, "获取用户信息失败");
      }
      // Log.i(TAG, resultJson.toString());

    } catch (UnsupportedEncodingException e) {
      Log.w(TAG, e.getLocalizedMessage());
    } catch (ClientProtocolException e) {
      Log.w(TAG, e.getLocalizedMessage());
    } catch (IOException e) {
      Log.w(TAG, e.getLocalizedMessage());
    } catch (ParseException e) {
      Log.w(TAG, e.getLocalizedMessage());
    } catch (JSONException e) {
      Log.w(TAG, e.getLocalizedMessage());
    }

    // 保存消息数
    try {
      newMsg.setStatus(resultJson.getInt("status"));
    } catch (Exception e) {
    }
    try {
      newMsg.setFollower(resultJson.getInt("follower"));
    } catch (Exception e) {
    }
    try {
      newMsg.setCmt(resultJson.getInt("cmt"));
    } catch (Exception e) {
    }
    try {
      newMsg.setDm(resultJson.getInt("dm"));
    } catch (Exception e) {
    }
    try {
      newMsg.setMention(resultJson.getInt("mention_status") + resultJson.getInt("mention_cmt"));
    } catch (Exception e) {
    }

    return newMsg;
  }