A、接口使用场景
在SDK中,还有一些功能不需要SDK提供UI交互的OpenApi接口,如获取用户信息、获取用户相册列表、发送分享(addshare)、发表说说、上传图片、创建相册等,更多功能接口请查看《API列表》。
这些功能统一通过调用Tencent类的request或requestAsync方法来实现。request和requestAsync这两个接口的功能相同,区别是一个是同步调用,一个是异步调用。
B、接口方法
异步:public void com.tencent.tauth.Tencent.requestAsync(String graphPath, Bundle params, String httpMethod, IRequestListener listener)
同步:public void com.tencent.tauth.Tencent.request(String graphPath, Bundle params, String httpMethod, IRequestListener listener) throws IOException, JSONException, HttpUtils.NetworkUnavailableException, HttpUtils.HttpStatusException
C、参数说明
调用增量授权接口的参数说明如下:
参数参数说明
graphPath要调用的接口名称,通过SDK中的UserInfo类获取宏定义。
params以K-V组合的字符串参数。Params是一个Bundle类型的参数,里面以键值对(Key-value)的形式存储数据,应用传入的邀请分享等参数就是通过这种方式传递给SDK,然后由SDK发送到后台。
httpMethodConstants#HTTP_GET}或 {@link Constants#HTTP_POST
listener回调接口,IUiListener实例。
D、示例代码
调用登录接口的示例代码如下:
IRequestListener listener = new IRequestListener () {
/**
*当请求完成后,会调用此方法。注意:这是个后台线程,不要在这个方法中修改UI。
* @param response 服务器返回的json结果
*/
public void onComplete(JSONObject response) {
xxx
}
public void onIOException(IOException e) {
xxx
}
};
final 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("sdkv", Constants.SDK_VERSION);
bundle.putString("status_version", android.os.Build.VERSION.SDK);
bundle.putString("sdkp", "a");
if (mToken != null && mToken.isSessionValid()) {
bundle.putString(Constants.PARAM_CONSUMER_KEY, mToken.getAppId());
bundle.putString(Constants.PARAM_ACCESS_TOKEN, mToken.getAccessToken());
bundle.putString(Constants.PARAM_OPEN_ID, mToken.getOpenId());
bundle.putString("appid_for_getting_config", mToken.getAppId());
}
// 从本地sharedpreference取出pf,填入params中
final SharedPreferences pfShared = Global.getContext().getSharedPreferences(Constants.PREFERENCE_PF,Context.MODE_PRIVATE);
final String pf = pfShared.getString(Constants.PARAM_PLATFORM_ID, Constants.DEFAULT_PF);
bundle.putString(Constants.PARAM_PLATFORM_ID, pf);
mTencent.requestAsync(mToken, Global.getContext(), GRAPH_VIP_RICH_INFO,params, Constants.HTTP_GET, listener);