我正在尝试使用 Retrofit2 创建请求。我使用标准库创建了请求:
path = "https://www.iii.com/?id="+id+"&data=";
query = "{\"name\":\""+name+"\",\"quantity\":20}";
最终链接是:
link = path+URLEncoder.encode(query, "UTF-8");
我尝试了不同的 Retrofit2 选项,但我不明白如何使用路径和带有 url 编码的查询将我的链接转换为 Retrofit2 链接?
最佳答案
您可以使用以下代码解析 GET 查询参数以进行改造:
@GET("https://www.iii.com")
Observable<ResponseBody> getSomething(
@Query("id") int id,
@Query("data") String data
);
Retrofit 将为您构建它。只需传递您的变量(假设您知道如何调用改造请求),改造将为您对其进行 url 编码。您可以引用这个链接:https://square.github.io/retrofit/2.x/retrofit/index.html?retrofit2/http/Query.html
Values are converted to strings using Retrofit.stringConverter(Type, Annotation[]) (or Object.toString(), if no matching string converter is installed) and then URL encoded. null values are ignored. Passing a List or array will result in a query parameter for each non-null item.
关于java - Retrofit2 复杂请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47991679/