Android 检索其Url查询
我在书中使用安卓应用程序对网络进行改造 这是查询URLAndroid 检索其Url查询,android,retrofit,Android,Retrofit,我在书中使用安卓应用程序对网络进行改造 这是查询URL https://www.googleapis.com/books/v1/volumes?q=subject:android 我想问我如何在改装中使用它,因为它的末尾包含冒号,我不知道怎么做。我是新手。最后我如何获得(主题:android)?我在网上搜索,但没有找到任何解决方案 我搜索了改装文档,但没有找到任何相关信息。尝试使用@Path注释 @GET(books/v1/volumes?q=subject:{subject}) Call&l
https://www.googleapis.com/books/v1/volumes?q=subject:android
我想问我如何在改装中使用它,因为它的末尾包含冒号,我不知道怎么做。我是新手。最后我如何获得(主题:android)?我在网上搜索,但没有找到任何解决方案
我搜索了改装文档,但没有找到任何相关信息。尝试使用
@Path
注释
@GET(books/v1/volumes?q=subject:{subject})
Call<ResponseBody> getBooks(@Path String subject);
@GET(books/v1/volumes?q=subject:{subject})
调用getBooks(@Path String subject);
您可以尝试@Query
接口:
public interface NetworkService {
@GET("books/v1/volumes")
Call<ResponseBody> getSubject(@Query(value = "q") String subjectName);
}
public static final String url = "https://www.googleapis.com/";
// onCreate()
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.build();
NetworkService client = retrofit.create(NetworkService.class);
Call<ResponseBody> call = client.getSubject("subject:android");
call.enqueue(new Callback<ResponseBody>()...);
public interface NetworkService {
@GET("books/v1/volumes")
Call<ResponseBody> getSubject(@QueryMap() Map<String, String> queryMap);
}
public static final String url = "https://www.googleapis.com/";
// onCreate()
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.build();
NetworkService client = retrofit.create(NetworkService.class);
Map<String, String> subjectMap = new HashMap<>();
subjectMap.put("q", "subject:android");
Call<ResponseBody> call = client.getSubject(subjectMap);
call.enqueue(new Callback<ResponseBody>()...);
用法:
public interface NetworkService {
@GET("books/v1/volumes")
Call<ResponseBody> getSubject(@Query(value = "q") String subjectName);
}
public static final String url = "https://www.googleapis.com/";
// onCreate()
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.build();
NetworkService client = retrofit.create(NetworkService.class);
Call<ResponseBody> call = client.getSubject("subject:android");
call.enqueue(new Callback<ResponseBody>()...);
public interface NetworkService {
@GET("books/v1/volumes")
Call<ResponseBody> getSubject(@QueryMap() Map<String, String> queryMap);
}
public static final String url = "https://www.googleapis.com/";
// onCreate()
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.build();
NetworkService client = retrofit.create(NetworkService.class);
Map<String, String> subjectMap = new HashMap<>();
subjectMap.put("q", "subject:android");
Call<ResponseBody> call = client.getSubject(subjectMap);
call.enqueue(new Callback<ResponseBody>()...);
不工作显示错误。原因:java.lang.IllegalArgumentException:URL查询字符串“q=subject:{subject}”不能有替换块。对于动态查询参数,请使用@query。