改型错误URL查询字符串不得包含由提供的动态值替换的块

时间:2019-09-21 10:42:10

标签: android retrofit2 android-json

我想通过改型获得JSON数据,但出现此错误

  

由于:java.lang.IllegalArgumentException:URL查询字符串必须   还没有更换块。对于动态查询参数,请使用@Query。

我的代码是

public interface ApiService {
    // this is link, WORD is dynamic string passing from activity
    // https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=WORD&format=json

        @GET("/w/api.php?action=query&list=search&srsearch={word}&format=json")
        Call<Search> getWordList(@Query({word}) String myText);
    }

还有这个

public class RetroClient { 
    private static final String ROOT_URL = "https://en.wikipedia.org/";

    private static Retrofit getRetrofitInstance() {
        return new Retrofit.Builder()
                .baseUrl(ROOT_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    public static ApiService getApiService() {
        return getRetrofitInstance().create(ApiService.class);
    }
}

通话

 Call<Search> call = apiService.getJsonData("myText");

 call.enqueue(new Callback<Search>() {
    @Override
    public void onResponse(Call<Search> call, Response<ApiService> Search) {
        //    int statusCode = response.code();
        if (response.body() != null) {
            translates = response.body().getMatches();
        }  

    }

    @Override
    public void onFailure(Call<Search> call, Throwable t) {

    }
 });
ApiService类中的

显示错误。如何通过单词进行链接,请

1 个答案:

答案 0 :(得分:2)

尝试一下

    @GET("/w/api.php")
    Call<Search> getWordList(
       @Query("action") String action,
       @Query("list") String list,
       @Query("srsearch") String srsearch,
       @Query("format") String format);

然后这样打电话

       Call<ApiService> call = apiService.getJsonData("query","search","<Word Which you want to pass>","json");