改进:URL查询字符串不能包含replace块

时间:2018-08-24 00:21:51

标签: java android android-studio retrofit

我正在尝试从IGDB api发出请求,但出现以下错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{leandro.com.gameloversapp/leandro.com.gameloversapp.Presentation.Main.MainActivity}: java.lang.IllegalArgumentException: URL query string "fields={fields}&filter[release_dates.platform][any]={platform}" must not have replace block. For dynamic query parameters use @Query.
        for method IGameApi.getGameByPlatform
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:169)
        at android.app.ActivityThread.main(ActivityThread.java:6568)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
     Caused by: java.lang.IllegalArgumentException: URL query string "fields={fields}&filter[release_dates.platform][any]={platform}" must not have replace block. For dynamic query parameters use @Query.
        for method IGameApi.getGameByPlatform
        at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:755)
        at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:746)
        at retrofit2.ServiceMethod$Builder.parseHttpMethodAndPath(ServiceMethod.java:303)
        at retrofit2.ServiceMethod$Builder.parseMethodAnnotation(ServiceMethod.java:248)
        at retrofit2.ServiceMethod$Builder.build(ServiceMethod.java:175)
        at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:170)
        at retrofit2.Retrofit$1.invoke(Retrofit.java:147)
        at java.lang.reflect.Proxy.invoke(Proxy.java:913)
        at $Proxy0.getGameByPlatform(Unknown Source)
        at leandro.com.gameloversapp.Server.GameServer.getGameByPlatform(GameServer.java:39)
        at leandro.com.gameloversapp.Presentation.Main.MainPresenter.getGamesBylatform(MainPresenter.java:34)
        at leandro.com.gameloversapp.Presentation.Main.MainActivity.onCreate(MainActivity.java:26)
        at android.app.Activity.performCreate(Activity.java:7016)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)

这些是ny类:

public interface IGameApi {

    @GET("/games/?fields=name,cover&filter[release_dates.platform][any]={platform}")
    @Headers("Accept:application/json")
    Call<List<Games>> getGameByPlatform(@Query("platform") int platform, @Header("user-key") String key);
}

GameServer.java

public class GameServer {

    private static IGameApi iGameApi;
    private static GameServer gameServer;
    Context context;


    public GameServer(Context mContext) {
        this.context = mContext;
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(context.getString(R.string.base_url))
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        iGameApi = retrofit.create(IGameApi.class);
    }

    public static GameServer getInstance(Context context){
        if(gameServer == null){
            gameServer = new GameServer(context);
        }
        return gameServer;
    }

    public void getGameByPlatform(int platform,String user_key, Callback<List<Games>> callback){
        Call<List<Games>> gamesCall = iGameApi.getGameByPlatform(platform, user_key);
        gamesCall.enqueue(callback);
    }
}

该请求在邮递员申请中有效。

https://api-endpoint.igdb.com/games/?fields=name,cover&filter[release_dates.platform][any]=4

有人可以帮我吗?

2 个答案:

答案 0 :(得分:0)

尝试一下

@GET("/games/?fields=name,cover")
    @Headers("Accept:application/json")
    Call<List<Games>> getGameByPlatform(@Query("filter[release_dates.platform][any]") int platform, @Header("user-key") String key);

答案 1 :(得分:-1)

使用@Path的{​​{1}}实例

@Query