Overriding special encoding characters

I use retrofit with gson instead of android, as it is faster and safer.

The problem is that the modification encodes special characters, such as =and ?, and the url that I use 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 to solve the problem using this gson method, but this did not work, most likely because it includes only the first part of the URL, and not the one that I send to the interface placeApi:

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?

+4
source share
1 answer

Use @EncodedPath. :

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

. , , , @EncodedPath , @PATH :

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

ref: https://square.imtqy.com/retrofit/2.x/retrofit/

+3

Source: https://habr.com/ru/post/1606966/


All Articles