2016-01-11 63 views
1

在爲「/1.1/lists/create.json」執行POST時指定回調的正確方法是什麼?TwitterApiClient:什麼是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

我不知道這是否是解決這一問題的最佳途徑,但它爲我工作。積分通過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) { } 
    });