I use Retrofit2, I want to send parameters to the POST server.

public interface ServerApi { @GET("Singleton") Call<String> getJSON(); @POST("Singleton") Call<String> senDDudes(@Query("author") String author); } 

I use this interface to send data. When entering the server, the data is received in the POST format, and then returned in the JSON format.

  Retrofit rt; rt=new Retrofit.Builder().baseUrl("http://10.0.2.2/").addConverterFactory(ScalarsConverterFactory.create()).addConverterFactory(GsonConverterFactory.create()).build(); ServerApi sapi=rt.create(ServerApi.class); Call<String> msg=sapi.senDDudes("Dude","bude"); msg.enqueue(new Callback<String>() { @Override public void onResponse(Call<String> call, Response<String> response) { System.out.println(response.body().toString()); } @Override public void onFailure(Call<String> call, Throwable t) { System.out.println(t.toString()); } }); 

The problem is that the data in the form of POST does not come to the server. There is a refund, but there is no reception. I check with echo $ _POST ['author'] on the server itself.

BUT if I use the @GET annotation instead of the @POST annotation, and output echo $ _GET ['author'] accordingly, the data is output.

In Retrofit, I'm new, so please consider this.

    1 answer 1

    Parameter annotated with @Query controls query query parameters (oddly enough). Therefore, in php, you can get this parameter using $_GET (as you know, the GET request has no body, and it is logical that the query parameters are retrieved from the query url -a).

    To send a body with a POST request:

     interface Foo { // Вся соль здесь @POST("/path") FooResponse postJson(@Body FooRequest body); } 

    Class of deserialized request body:

     public class FooRequest { final String foo; final String bar; FooRequest(String foo, String bar) { this.foo = foo; this.bar = bar; } } 

    Make a request:

     FooResponse = foo.postJson(new FooRequest("kit", "kat")); 
    • That is, I understand correctly that I will not be able to receive $ _POST ['data'] data? - Dmitriy
    • @ Dmitri do as in response and try. - Peter Samokhin
    • Use bundles GET + Query , POST + Body and there will be no confusion at the reception. - Eugene Krivenja