Last active
August 13, 2016 06:29
-
-
Save Lzyct/15fe202b8dc5791b1f956690832469d6 to your computer and use it in GitHub Desktop.
Get return value from Retrofit 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| *Create interface to delivery status | |
| * | |
| */ | |
| public interface StatusInterface{ | |
| void returnResponse(int status); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| *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