private void updateTokenView(boolean hasExisted) {
   String date =
       new SimpleDateFormat("yyyy/MM/dd HH:mm:ss")
           .format(new java.util.Date(mAccessToken.getExpiresTime()));
   String format = context.getString(R.string.weibosdk_demo_token_to_string_format_1);
   String message = String.format(format, mAccessToken.getToken(), date);
 }
 /**
  * 当前用户
  *
  * @param context context
  */
 public static void start(Context context) {
   Intent intent = new Intent(context, UserDetailActivity.class);
   Oauth2AccessToken token = AccessTokenKeeper.readAccessToken(context);
   intent.putExtra(INTENT_UID, token.getUid());
   intent.putExtra(INTENT_TOKEN, token.getToken());
   context.startActivity(intent);
 }
  /**
   * 保存 Token 对象到 SharedPreferences。
   *
   * @param context 应用程序上下文环境
   * @param token Token 对象
   */
  public static void writeAccessToken(Context context, Oauth2AccessToken token) {
    if (null == context || null == token) {
      return;
    }

    SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND);
    Editor editor = pref.edit();
    editor.putString(KEY_UID, token.getUid());
    editor.putString(KEY_ACCESS_TOKEN, token.getToken());
    editor.putLong(KEY_EXPIRES_IN, token.getExpiresTime());
    editor.commit();
  }
Beispiel #4
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;
  }
Beispiel #5
0
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_welcom);

    Oauth2AccessToken oauth2AccessToken = AccessTokenKeeper.readAccessToken(this);
    if (oauth2AccessToken != null
        && !TextUtils.isEmpty(oauth2AccessToken.getUid())
        && !TextUtils.isEmpty(oauth2AccessToken.getToken())
        && !TextUtils.isEmpty(oauth2AccessToken.getRefreshToken())) {
      startActivity(new Intent(this, MainActivity.class));
      this.finish();
    } else {
      mAuthInfo = new AuthInfo(this, Constants.APP_KEY, Constants.REDIRECT_URL, Constants.SCOPE);
      mSsoHandler = new SsoHandler(WelcomActivity.this, mAuthInfo);
      mSsoHandler.authorize(new AuthListener());
    }
  }
Beispiel #6
0
 /**
  * 查询accesstoken是否过期
  *
  * @param context
  * @param token
  * @return
  */
 public static Boolean isTokenOverTime(Context context, Oauth2AccessToken token) {
   // 组织post参数:此处只实现最简单的发送消息,只填两个参数,其他见http://open.weibo.com/wiki/2/statuses/friends_timeline
   List<BasicNameValuePair> params = new LinkedList<BasicNameValuePair>();
   params.add(new BasicNameValuePair("access_token", token.getToken()));
   try {
     JSONObject resultJSON = doPost(context, params, Contants.GET_TOKEN_INFO_URL);
     try {
       String timeRest = resultJSON.getString("expire_in");
       Log.w(TAG, "Token rest time :" + timeRest);
       long time = Integer.parseInt(timeRest);
       if (time < 1000) {
         return false;
       }
     } catch (JSONException e) {
       e.printStackTrace();
     }
   } catch (Exception e) {
   }
   return true;
 }
Beispiel #7
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;
  }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_forward_comment);

    cid = getIntent().getStringExtra("cid");
    id = getIntent().getStringExtra("id");
    if (TextUtils.isEmpty(cid) || TextUtils.isEmpty(id)) {
      finish();
      return;
    }

    mAccessToken = AccessTokenKeeper.readAccessToken(getApplicationContext());
    mParams = new WeiboParameters(Constants.APP_KEY);
    mParams.put("access_token", mAccessToken.getToken());
    inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

    titleBar = (TitleBar) findViewById(R.id.title_bar);
    replyContent = (EditText) findViewById(R.id.et_forward_content);
    countTV = (TextView) findViewById(R.id.tv_count);
    sameTime = (CheckBox) findViewById(R.id.cb_same_time);
    faceKeyboard = (ImageButton) findViewById(R.id.btn_face_keyboard);
    send = (ImageButton) findViewById(R.id.btn_send);
    faceGrid = (GridView) findViewById(R.id.gv_face);
    inputRoot = (RelativeLayout) findViewById(R.id.ll_input_root);

    sameTime.setVisibility(View.GONE);
    titleBar.setTitle("回复");
    titleBar.setBackClickListener(this);
    replyContent.setHint("回复");
    replyContent.addTextChangedListener(this);
    replyContent.setSelection(0);
    replyContent.setOnClickListener(this);
    faceKeyboard.setOnClickListener(this);
    faceGrid.setAdapter(new EmotionsGridAdapter(this));
    faceGrid.setOnItemClickListener(this);
    send.setOnClickListener(this);
    inputRoot.setOnClickListener(this);
  }
Beispiel #9
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;
  }