修改来自 url 的呼叫 ID

Pre*_*ren 1 android retrofit

我如何在改造网络调用中使用这些查询并将其显示在类别明智的结果中

https://api.themoviedb.org/3/genre/{genre_id}/movies
Run Code Online (Sandbox Code Playgroud)

Sai*_*key 5

也许这可以帮助:

接口类

public interface TheApiInterface{
     @GET("url/bits/until/{path_variable}/then/more/url")
     Call<TheThingResponse> getTheThing(@Path("path_variable") String var);
}
Run Code Online (Sandbox Code Playgroud)

活动或其他:

public class ThePlaceYoureCallingItFrom {

    //set up the api interface and http client 
    public TheApiInterface getApi(){
        String endpoint = "https://api.root.site/api/";
        //set up retrofit object
        return new Retrofit.Builder().baseUrl(endpoint)
                //add chosen converter factory for pojo serialization
                .addConverterFactory(GsonConverterFactory.create())
                //add the OKHTTP client
                .client(new OkHttpClient.Builder().build())
                //now gimme
                .build().create(TheApiInterface.class);
    }

    public void callGetTheThing(){
        //create call
        Call<TheThingResponse> call = getApi().getTheThing("somePathVar");
        //set callback
        ThingResponseCallback callback = new ThingResponseCallback(this, THING_RESPONSE_INTENT_FILTER);
        //fire
        call.enqueue(callback);
    }
}
Run Code Online (Sandbox Code Playgroud)

回调:

public class ThingResponseCallback implements Callback<TheThingResponse>{
     @Override
     public void onResponse(Call<TheThingResponse> call, Response<TheThingResponse> response) {
        if (response.isSuccessful() && response.body() != null) {
            Log.i(TAG, "onResponse: success: theResponseFieldIWant1: " + response.theResponseFieldIWant1;);
        } else {
            Log.i(TAG, "onResponse: something went wrong with the response object " +response.body());
        }
    }

    @Override
    public void onFailure(Call<TheThingResponse> call, Throwable t) {
         Log.i(TAG, "onFailure: to: " + call.request().url() + " req " + call.request());
    }
}
Run Code Online (Sandbox Code Playgroud)

回复 pojo:

public class TheThingResponse{
    @SerializedName("theJsonKeyOfTheFieldReturnedInServerResponse1")
    public String theResponseFieldIWant1;

    @SerializedName("theJsonKeyOfTheFieldReturnedInServerResponse2")
    public String theResponseFieldIWant2;

    @SerializedName("theJsonKeyOfTheFieldReturnedInServerResponse3")    
    public String theResponseFieldIWant3;

    @SerializedName("theJsonKeyOfTheFieldReturnedInServerResponse4")    
    public String theResponseFieldIWant4;
}
Run Code Online (Sandbox Code Playgroud)

您收到的 JSON 如下所示:

 {
"theJsonKeyOfTheFieldReturnedInServerResponse1": "the value I wanted 1",
"theJsonKeyOfTheFieldReturnedInServerResponse2": "the value I wanted 2",
"theJsonKeyOfTheFieldReturnedInServerResponse3": "the value I wanted 3",
"theJsonKeyOfTheFieldReturnedInServerResponse4": "the value I wanted 4"
 }
Run Code Online (Sandbox Code Playgroud)

但您可以为更复杂的 JSON 构建更复杂的 POJO。

我发现让我的 POJO 共享一个可序列化的父类很有用,使它们可以在回调中轻松移动,但您也可以在这里很容易地使用 ContentProvider 并将一些行插入数据库或类似的东西如果您想拥有更永久的存储空间。

但请记住,这都是异步的 - 如果您想要同步 Retrofit 调用,您可以使用 call.execute()