tencent API(二) 发表说说

上次已经讲到了怎么登入,下面演示下登入过后如何发说说(文字,图片,或视频)

1.发表文字说说

      public static final String GRAPH_ADD_TOPIC = "shuoshuo/add_topic";

(1)要发说说,肯定是要传递验证信息,首先要创建出一个信息的载体,这路是利用Bundle传递信息

       

private Bundle composeCGIParams() {
    	Bundle bundle = new Bundle();
    	//填上通用参数, 后台可能会对不同版本的请求做策略控制
    	bundle.putString("format", "json");
        bundle.putString("status_os", android.os.Build.VERSION.RELEASE);
    	bundle.putString("status_machine", android.os.Build.MODEL);
    	bundle.putString("status_version", android.os.Build.VERSION.SDK);
    	bundle.putString("sdkv", Constants.SDK_VERSION);
    	bundle.putString("sdkp", "a");
    	//读取用户登入信息
    	if (mToken != null && mToken.isSessionValid()) {
    		bundle.putString(Constants.PARAM_ACCESS_TOKEN, mToken.getAccessToken());
    		bundle.putString(Constants.PARAM_CONSUMER_KEY, mToken.getAppId());
    		bundle.putString(Constants.PARAM_OPEN_ID, mToken.getOpenId());
        }
    	
        // 此字段在openUrl2中取出,在获取config时填入,但不放在cgi请求参数中
    	bundle.putString("appid_for_getting_config", mToken.getAppId());

        // 从本地sharedpreference取出pf,填入params中
        SharedPreferences pfShared = this.getApplicationContext().getSharedPreferences(
                Constants.PREFERENCE_PF,
                Context.MODE_PRIVATE);
        String pf = pfShared.getString(Constants.PARAM_PLATFORM_ID, Constants.DEFAULT_PF);
        bundle.putString(Constants.PARAM_PLATFORM_ID, pf);
    	return bundle;
    }

 (2).传递要发的说说内容

   

/**
 * 发表说说
*/
private void onClickAddTopic() {
		
       Bundle params = composeCGIParams();
       params.putString("richtype", "2");// 发布心情时引用的信息的类型。1表示图片;
                                         // 2表示网页; 3表示视频。
       params.putString("richval",
               ("http://www.qq.com" + "#" + System.currentTimeMillis()));// 发布心情时引用的信息的值。有richtype时必须有richval
       params.putString("con", "测试:调用腾讯测试api成功!");// 发布的心情的内容。
       params.putString("lbs_nm", "美国硅谷");// 地址文
       params.putString("lbs_x", "0-360");// 经度。请使用原始数据(纯经纬度,0-360)。
       params.putString("lbs_y", "0-360");// 纬度。请使用原始数据(纯经纬度,0-360)。
       params.putString("lbs_id", "360");// 地点ID。lbs_id与lbs_idnm通常一起使用,来明确标识一个地址。
       params.putString("lbs_idnm", "微软");// 地点名称。lbs_id与lbs_idnm通常一起使用,来明确标识一个地址。
       mTencent.requestAsync(GRAPH_ADD_TOPIC, params,
               Constants.HTTP_POST, new BaseApiListener("add_topic", true), null);
       mProgressDialog.show();
   }

 3.再创建一个监听类来结束返回内容

private class BaseApiListener implements IRequestListener {
		private String mScope = "all";
		private Boolean mNeedReAuth = false;

		public BaseApiListener(String scope, boolean needReAuth) {
			mScope = scope;
			mNeedReAuth = needReAuth;
		}

		@Override
		public void onComplete(JSONObject response) {
			// TODO Auto-generated method stub
			showResult("onComplete:", response.toString());
			doComplete(response);
		}

		protected void doComplete(JSONObject response) {
			try {
				int ret = response.getInt("ret");
				if (ret == 100030) {
					if (mNeedReAuth) {
						Runnable r = new Runnable() {
							public void run() {
								mTencent.reAuth(MainActivity.this, mScope,
										new BaseUiListener());
							}
						};
						MainActivity.this.runOnUiThread(r);
					}
				}
			} catch (JSONException e) {
				e.printStackTrace();
				Log.e("toddtest", response.toString());
			}

		}

		@Override
		public void onIOException(final IOException e) {
			showResult("IRequestListener.onIOException:", e.getMessage());
		}

		@Override
		public void onMalformedURLException(final MalformedURLException e) {
			showResult("IRequestListener.onMalformedURLException", e.toString());
		}

		@Override
		public void onJSONException(final JSONException e) {
			showResult("IRequestListener.onJSONException:", e.getMessage());
		}

		@Override
		public void onConnectTimeoutException(ConnectTimeoutException arg0) {
			showResult("IRequestListener.onConnectTimeoutException:",
					arg0.getMessage());

		}

		@Override
		public void onSocketTimeoutException(SocketTimeoutException arg0) {
			showResult("IRequestListener.SocketTimeoutException:",
					arg0.getMessage());
		}

		@Override
		public void onUnknowException(Exception arg0) {
			showResult("IRequestListener.onUnknowException:", arg0.getMessage());
		}

		@Override
		public void onHttpStatusException(HttpStatusException arg0) {
			showResult("IRequestListener.HttpStatusException:",
					arg0.getMessage());
		}

		@Override
		public void onNetworkUnavailableException(
				NetworkUnavailableException arg0) {
			showResult("IRequestListener.onNetworkUnavailableException:",
					arg0.getMessage());
		}

	}

}

  
tencent API(二)  发表说说
 2.发表视频说说

 public static final String GRAPH_ADD_SHARE = "share/add_share";

相关推荐