Send and receive data with Retrofiten AndroidStudio

2

I have a project that requires to send and receive data from a server. I have been recommended to use Retrofit , but I have only managed to send the data to the server in JSON form. To be honest, I have no idea how to implement data reception (I would receive an int and a string in JSON).

retrofit: link

Main

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {
            Button button = (Button)findViewById(R.id.button);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final TextView hora  = (TextView) findViewById(R.id.hora);
                    final int sharehora = Integer.valueOf(hora.getText().toString());
                    final TextView missatge  = (TextView) findViewById(R.id.text);
                    final String shareMissatge = missatge.getText().toString();
                    Retrofit retrofit = new Retrofit.Builder()
                            .baseUrl("http://www.google.com/")
                            .addConverterFactory(GsonConverterFactory.create())
                            .build();
                    CostumBody costumBody = new CostumBody();
                    costumBody.setMsg(shareMissatge);
                    costumBody.setTime(sharehora);
                    giapi service = retrofit.create(giapi.class);
                    service.Calltomyserver(costumBody);

                }
            });
        }
        catch(Exception ex){
            Log.e("error!", String.valueOf(ex.getMessage()));
        }
    }
}

GIAPI

public interface giapi {
    @POST("/")
    Call<Void> Calltomyserver (@Body CostumBody user);
}

custumbody

public class CostumBody {
    public String msg;
    public int time;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public int getTime() {
        return time;
    }

    public void setTime(int time) {
        this.time = time;
    }

}
    
asked by Pau Mateu i Jordi 09.10.2016 в 00:03
source

1 answer

0

In order to advance your question, the interface understands that it is not handling the answer because it lacks a Callback just receive it.

This would be an example

public interface giapi {
    @FormUrlEncoded
    @POST("/endpointQueDevuelvaJSON")
    public void getMisDatos(@Field("time") 
int time, Callback<CostumBody> response);
}

In this case I am sending the field time , but it is not clear to me if you need to do it or the json resolves that you return it in another way (so as not to always send the same I mean.

In any case what matters is CallBack . I would check the monitor if it returns ok the json before moving on to MainActivity .

I clarify that at least this is how I do it, I do not know if there is another better way but I use retrofit without problems in this way.

    
answered by 09.10.2016 в 13:49