Mr Squidr
Mr Squidr

Reputation: 143

Android Retrofit2 unable to get POST request working

I'm unable to get POST http request to work. It seems like I'm not using retrofit2 library correctly, but from my research what I have should be working.

This is what my simplified object Class looks like:

public class Tournament {
    private String name;
    private int nOfGames;
    private String game;
    private String userId;

    public Tournament(String name, int nOfGames, String game, String userId) {
        this.name = name;
        this.nOfGames = nOfGames;
        this.game = game;
        this.userId = userId;
    }
}

I tried two approaches:

approach 1:

retrofit interface:

@Headers({"Content-Type: application/json;charset=UTF-8"})
@POST("TournamentApi/")
Single<Tournament> createTournament(@Body Tournament tournament);

use:

Tournament tournament = new Tournament(
        "some name",
        2,
        "some game",
        "1");

api.createTournament(tournament);

approach 2:

retrofit interface:

@FormUrlEncoded
@POST("TournamentApi/")
public void insertTournament(
        @Field("Name") String name,
        @Field("NOfGames")  int nOfGames,
        @Field("Game") String game,
        @Field("UserId") String userId,
        Callback<Response> callback);

use:

api.insertTournament(
        tournamentName.getText().toString(),
        Integer.parseInt(bestOfN.getText().toString()),
        gameName.getText().toString(),
        "1",
        new Callback<Response>() {
            @Override
            public void onResponse(Call<Response> call, Response<Response> response) {
                String output = call.toString();
                String responze = response.toString();
            }

            @Override
            public void onFailure(Call<Response> call, Throwable t) {
                String output = call.toString();
            }
        });

First approach doesn't do anything at all, no errors, nothing. I even tried using HttpLoggingInterceptor but nothing shows up at all. No requests are made it seems. Second approach crashes my app entirely because I get No Retrofit annotation found. (parameter #1) error but I've seen other post same snippets without any annotation on callback that were supposedly working.

I also use retrofit2 for get requests which are working just fine, it's just post requests that I'm not able to get working.

Finally here is what works flawlessly (inserts entry into database) if I use it with Postman:

POST http://192.168.1.124:5001/api/TournamentApi
{
    "Name": "blabla_tournament",
    "NOfGames": 3,
    "Game": "some_game",
    "UserId": "5"
}

I would suspect it's something to do with localhost but since get requests are working just like they should be I'm pretty confident the issue is I'm using retrofit2 wrong.

Upvotes: 0

Views: 58

Answers (2)

Morteza
Morteza

Reputation: 177

You must change : ' @Body Tournament tournament'
To : '@Body JsonObject tournament'
That's will be work.

Upvotes: 0

Avijit Acharjee
Avijit Acharjee

Reputation: 31

Try something like this. @Headers({"Content-Type: application/json;charset=UTF-8"}) @POST("TournamentApi") Single createTournament(@Body Tournament tournament);`

`

Upvotes: 0

Related Questions