C. Skjerdal
C. Skjerdal

Reputation: 3099

How to get the response from Retrofit POST request

I am fixing a broken project and I am trying to fix one of their features.

I need to solve using Retrofit to make a post with data to then receive a response of information.

public interface GetDataService {

@FormUrlEncoded
@POST("pocketdeal/togetallfavorites")
Call<Favourite> getFavorite();

}

The method for getting the favorite information

     public void MyFavoriteDeals4(String title, String body) {

    //(title, body, 1) is the method for adding things
    mAPIService.getFavorite().enqueue(new Callback<Post>() {
        @Override
        public void onResponse(Call<Post> call, Response<Post> response) {

            if(response.isSuccessful()) {
                showResponse(response.body().toString());
                Log.e("Favorites", "Successful");
            }
        }

        @Override
        public void onFailure(Call<Post> call, Throwable t) {
            Log.e("Favorites", "Failure");
        }
    });
}

I get no response or failure nor have I used all the information I need. Additionally is it bad to post my API information online?

The information I need to use..

My Post Data: lat=37.785834&long=-122.406417&android_id=1AC7C092-AC45-419F-AFED-3D2FEE473750&timezone=America/Vancouver

My URL: removed for privacy

Request Header: Content-Type with application/x-www-form-urlencoded

I have tested all of this in an API tester online so I know it produces correct results if done properly

Upvotes: 1

Views: 2764

Answers (1)

Facundo Larrosa
Facundo Larrosa

Reputation: 3389

As you are using @FormUrlEncoded you should add your params with the Field annotation:

public interface GetDataService {

@FormUrlEncoded
@POST("pocketdeal/togetallfavorites")
Call<Favourite> getFavorite(@Field("lat") Long lat, @Field("long") Long lng, @Field("android_id") String androidId, @Field("timezone") String timezone);

}

And you should initialize your retrofit call like this:

Call<Favourite> call2 = favouriteInterfacemAPIService.getFavorite(37.785834, -122.406417, "1AC7C092-AC45-419F-AFED-3D2FEE473750", "America/Vancouver");

Upvotes: 2

Related Questions