enter image description here For some unknown reason, I do not receive data from the server

public interface API { @FormUrlEncoded @POST("/api/index.php") Call<String> getId(@Field("enter") String s);} 

I want to send a Post request to the address

https: // developerteam.ml // api / index.php

with the string value enter (key) s (value)

ID should come back

  private Gson gson = new GsonBuilder().create(); private Retrofit retrofit = new Retrofit.Builder() .addConverterFactory(GsonConverterFactory.create(gson)) .baseUrl("https://developerteam.ml") .build(); private API api = retrofit.create(API.class); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Call<String> call = api.getId("1"); call.enqueue(new Callback<String>() { @Override public void onResponse(Call<String> call, Response<String> response) { ID = gson.toJson(response.body().toString()); } @Override public void onFailure(Call<String> call, Throwable t) { } }); 

Content response.body:

{serializeNulls: falsefactories: [Factory [typeHierarchy = com.google.gson.JsonElement, adapter = com.google.gson.internal.bind.TypeAdapters $ 25 @ ec799a3], com.google.gson.internal.bind.ObjectTypeAdapter$1@2de44a0 , com.google.gson.internal.Excluder@54c2859, Factory [type = java.lang.String, adapter = com.google.gson.internal.bind.TypeAdapters $ 13 @ 38e7c1e], Factory [type = java.lang.Integer + int, adapter = com.google.gson.internal.bind.TypeAdapters $ 7 @ ab02cff], Factory [type = java.lang.Boolean + boolean, adapter = com.google.gson.internal.bind.TypeAdapters $ 3 @ 6f30acc] .....

enter image description here

enter image description here

  • On the server exactly everything works? Check with postman whether the request is being executed. - Nikotin N
  • via postman comes {"id": "50", "password": "****"} - Heaven
  • response.body () what does it contain? - Nikotin N
  • most likely the error is that you wait for the response.body () string and try to convert it to JSON. But you connected GsonConverter which already does it for you. Those. ewsponse.body () should not be a string but a model that contains the ID and PASSWORD. - Nikotin N

1 answer 1

Try this:

Interface:

 public interface API { @FormUrlEncoded @POST("/api/index.php") Call<MyClass> getId(@Field("enter") String s); } 

Request Result Class:

 public class MyClass{ String password; int id; public int getID(){return id} } 

Request:

 Call<MyClass> call = api.getId("1"); call.enqueue(new Callback<MyClass>() { @Override public void onResponse(Call<MyClass> call, Response<MyClass> response) { ID = response.body().getID(); } @Override public void onFailure(Call<MyClass> call, Throwable t) { } }); 
  • It did not help, ID: 0 and there only Call <MyClass> all (the compiler cursed and asked to change) - Heaven
  • what did you swear at and what did you request to change? - Nikotin N
  • Cursed at Call <String> changed to Call <MyClass> - Heaven
  • Try changing the id type from int to String - Nikotin N 5 minutes ago - Nikotin N
  • Changed, now ID: null - Heaven