【问题标题】:Retrofit error URL query string must not have replace block by provided dynamically values改造错误 URL 查询字符串不能通过动态提供的值替换块
【发布时间】:2020-01-22 04:02:26
【问题描述】:

我想使用改造获取 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 类中显示错误。如何传递word链接,请

【问题讨论】:

    标签: android retrofit2 android-json


    【解决方案1】:

    试试这个

        @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");
    

    【讨论】:

      猜你喜欢
      • 2018-02-04
      • 2014-08-27
      • 2017-12-12
      • 2019-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-27
      • 2012-07-10
      相关资源
      最近更新 更多