问题描述
我使用gson而不是android进行改造,因为它更快,更安全.
I am using retrofit with gson instead of android since its faster and more secure.
问题是改型编码的是特殊字符,例如=
和?
,而我正在使用的url无法解码这些字符.
The problem is that retrofit is encoding special characters like =
and ?
, and the url I'm using cannot decode these characters.
这是我的代码:
api类:
public interface placeApi {
@GET("/{id}")
public void getFeed(@Path("id") TypedString id, Callback<PlaceModel> response);
}
主类:
String url = "http://api.beirut.com/BeirutProfile.php?";
String next = "profileid=111";
//Creating adapter for retrofit with base url
RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(url).setRequestInterceptor(requestInterceptor).build();
//Creating service for the adapter
placeApi placeApi = restAdapter.create(placeApi.class);
placeApi.getFeed(id, new Callback<PlaceModel>() {
@Override
public void success(PlaceModel place, Response response) {
// System.out.println();
System.out.println(response.getUrl());
name.setText("Name: " + place.getName());
}
@Override
public void failure(RetrofitError error) {
System.out.println(error.getMessage());
}
});
我尝试使用此gson方法解决问题,但没有成功,很可能是因为它仅包含url的第一部分,而不包含我发送给placeApi
界面的那一部分:
I tried solving the problem using this gson method but it didn't work, most probably because it only includes only the first part of the url and not the one I am sending to the placeApi
interface:
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
并在创建restadapter时添加了它:
and added this when creating the restadapter:
RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(url).setRequestInterceptor(requestInterceptor).setConverter(new GsonConverter(gson)).setConverter(new GsonConverter(gson)).build();
请帮忙吗?
推荐答案
您必须使用Use @EncodedPath.像这样:
You must use Use @EncodedPath. like this:
public interface placeApi {
@GET("/{id}")
public void getFeed(@EncodedPath("id") TypedString id,
Callback<PlaceModel> response);
}
注意:上面的方法有效,但现在我正在查看文档,似乎@EncodedPath已过时,因此请使用@PATH及其参数:
Note: The above works but now I am looking at the doc and it seems that the @EncodedPath is deprecated so use @PATH with its parameter instead:
public interface placeApi {
@GET("/{id}")
public void getFeed(@Path("id", encode=false) TypedString id,
Callback<PlaceModel> response);
}
ref: https://square.github.io/retrofit/2.x/retrofit/
这篇关于改造编码特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!