Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
951 views
in Technique[技术] by (71.8m points)

android - Retrofit encoding special characters

I am using retrofit with gson instead of android since its faster and more secure.

The problem is that retrofit is encoding special characters like = and ?, and the url I'm using cannot decode these characters.

This is my code:

api class:

public interface placeApi {

@GET("/{id}")
public void getFeed(@Path("id") TypedString id, Callback<PlaceModel> response);
}

Main class:

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());
        }
    });

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();

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();

Any help please?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You must use Use @EncodedPath. like this:

public interface placeApi {
@GET("/{id}")
public void getFeed(@EncodedPath("id") TypedString id,
   Callback<PlaceModel> response);
}

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/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...