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;
}
}