Skip to content

Instantly share code, notes, and snippets.

@Lzyct
Last active August 13, 2016 06:29
Show Gist options
  • Select an option

  • Save Lzyct/15fe202b8dc5791b1f956690832469d6 to your computer and use it in GitHub Desktop.

Select an option

Save Lzyct/15fe202b8dc5791b1f956690832469d6 to your computer and use it in GitHub Desktop.
Get return value from Retrofit 2
StatusResponse statusResponse = new StatusResponse(param1,param2,new StatusInterface(){
@Override
public void returnResponse(int status){
switch(status){
case 1: //do your stuff
break;
case 2: //do your stuff
break;
default:
break;
}
}
}
/*
*Create interface to delivery status
*
*/
public interface StatusInterface{
void returnResponse(int status);
}
/*
*Create class to fill data response
*
*/
public class StatusResponse{
private StatusInterface statusInterface;
public StatusResponse(String param1,String param2,StatusInterface statusInterface){
this.statusInterface = statusInterface;
APISample apiSample = APISample.clientSample.create(APISample.class);
Call<Sample> Sample = apiSample.getSample();
Sample.enqueue(new Callback<Sample>() {
@Override
public void onResponse(Call<Sample> call, Response<Sample> response) {
try {
switch (response.code()) {
case 200:
statusInterface.returnResponse(1);
break;
case 403:
statusInterface.returnResponse(2);
break;
default:
break;
}
} catch (Exception e) {
statusInterface.returnResponse(3);
}
}
@Override
public void onFailure(Call<Sample> call, Throwable t) {
statusInterface.returnResponse(4);
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment