TwitterApiClient:什么是POST的回调签名?

时间:2016-01-11 18:49:44

标签: android twitter callback http-post retrofit

为" /1.1/lists/create.json"执行POST时指定回调的正确方法是什么?

以下是Twitter API文档:https://dev.twitter.com/rest/reference/post/lists/create

MyTwitterApiClient

import com.twitter.sdk.android.core.Callback;
import com.twitter.sdk.android.core.TwitterApiClient;
import com.twitter.sdk.android.core.TwitterSession;
import com.twitter.sdk.android.core.models.User;

import retrofit.http.Field;
import retrofit.http.GET;
import retrofit.http.POST;
import retrofit.http.Query;

class MyTwitterApiClient extends TwitterApiClient {
    public MyTwitterApiClient(TwitterSession session) {
        super(session);
    }

    public CustomService getCustomService() {
        return getService(CustomService.class);
    }
}

interface CustomService {
    @GET("/1.1/users/show.json")
    void show(@Query("user_id") long id, Callback<User> cb);

    @POST("/1.1/lists/create.json")
    // I was incorrectly using @Field instead of @Query, be careful!
    void createList(@Query("name") String name, @Query("mode") String mode, Callback cb);
}

1 个答案:

答案 0 :(得分:1)

我不知道这是否是解决此问题的最佳方法,但它对我有用。积分通过How can I return String or JSONObject from asynchronous callback using Retrofit?转到@tposchel。

MyTwitterApiClient.java 将回调类型更改为JsonElement

interface CustomService {
    @GET("/1.1/users/show.json")
    void show(@Query("user_id") long id, Callback<User> cb);

    @POST("/1.1/lists/create.json")
    void createList(@Query("name") String name, @Query("mode") String mode, Callback<JsonElement> cb);
}

<强> MainActivity.java

MyTwitterApiClient api = new MyTwitterApiClient(session);
api.getCustomService().createList("MyListName", "private", new Callback<JsonElement>() {
    @Override
    public void success(Result<JsonElement> result) {
       Log.d(TAG, " [success] status: " + result.response.getStatus());
       JsonElement je = result.data;
       Log.d(TAG, " [success] data: " + je.getAsJsonObject().toString());
    }
    @Override
       public void failure(TwitterException e) { }
    });