简体   繁体   中英

Returning null response from Retrofit in Android

Am new to Retrofit and API Integration. Am trying to display response in a toast. Am using Retrofit for an API call. But the response from Retrofit is showing null. There is no response instead null. I don't Know may be am doing in a wrong way. Here is my code. Please Help me

Below is sample API

Params:
{
  "DeviceID": "Hawqr-jagadish-test-device",
  "DeviceType": "1",
  "DeviceName": "Jagadish Device",
  "AppVersion": 1
}
Response:
{
  "success": true,
  "extras": {
    "Status": "Device Splash Screen Completed Successfully",
    "Env_Type": 2,
    "ApiKey": "4ae3a7e5-2622-4376-bc0d-a0ccd6c0405a",
    "Whether_Latest_Version": true,
    "SocketIO_Data": {
      "socket_host": "https://xsocket.hawqr.com",
      "socketjs_link": "https://xsocket.hawqr.com/socket.io/socket.io.js"
    }
  }
}

This My Main Activity. In main activity i have call API

      @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
        
                getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Intent intent = new Intent(MainActivity.this,LoginActivity.class);
                        startActivity(intent);
                        finish();
                    }
                },SPLASH_SCREEN );
        
         String DeviceID = "Hawqr-jagadish-test-device";
                String DeviceType = "1";
                String DeviceName = "Jagadish Device";
                Integer AppVersion =  1 ;
        
         SplashRequest splashRequest = new SplashRequest();
                splashRequest.setDeviceID(DeviceID);
                splashRequest.setDeviceType(DeviceType);
                splashRequest.setDeviceName(DeviceName);
                splashRequest.setAppVersion(AppVersion);
        
        
                SplashScreen(splashRequest);
         }
 public void SplashScreen(SplashRequest splashRequest){

        UserService userService = ApiClient.getRetrofit().create(UserService.class);

        Call<SplashResponse> splashResponseCall = userService.splashScreen(splashRequest);

        splashResponseCall.enqueue(new Callback<SplashResponse>() {
            @Override
            public void onResponse(Call<SplashResponse> call, Response<SplashResponse> response) {

                Gson gson = new Gson();
                String successResponse = gson.toJson(response.body());

                Toast.makeText(MainActivity.this,successResponse,Toast.LENGTH_LONG).show();

            }

            @Override
            public void onFailure(Call<SplashResponse> call, Throwable t) {
 
             String msg = t.getLocalizedMessage();
            Toast.makeText(MainActivity.this,msg,Toast.LENGTH_SHORT).show();

            }
        });
    }
}

These are my Model classes for Parameters

 public class SplashRequest {
        public String DeviceID;
        public String DeviceType;
        public String DeviceName;
        public Integer AppVersion;
    
        public String getDeviceID() {
            return DeviceID;
        }
    
        public void setDeviceID(String deviceID) {
            DeviceID = deviceID;
        }
    
        public String getDeviceType() {
            return DeviceType;
        }
    
        public void setDeviceType(String deviceType) {
            DeviceType = deviceType;
        }
    
        public String getDeviceName() {
            return DeviceName;
        }
    
        public void setDeviceName(String deviceName) {
            DeviceName = deviceName;
        }
    
        public Integer getAppVersion() {
            return AppVersion;
        }
    
        public void setAppVersion(Integer appVersion) {
            AppVersion = appVersion;
        }

Another Model Class

 public String ApiKey;
    public String Env_Type;
    public String Status;
    public String Whether_Latest_Version;

    public SplashResponse(String apiKey, String env_Type, String status, String whether_Latest_Version) {
        ApiKey = apiKey;
        Env_Type = env_Type;
        Status = status;
        Whether_Latest_Version = whether_Latest_Version;
    }



    public String getApiKey() {
        return ApiKey;
    }

    public void setApiKey(String apiKey) {
        ApiKey = apiKey;
    }

    public String getEnv_Type() {
        return Env_Type;
    }

    public void setEnv_Type(String env_Type) {
        Env_Type = env_Type;
    }

    public String getStatus() {
        return Status;
    }

    public void setStatus(String status) {
        Status = status;
    }

    public String getWhether_Latest_Version() {
        return Whether_Latest_Version;
    }

    public void setWhether_Latest_Version(String whether_Latest_Version) {
        Whether_Latest_Version = whether_Latest_Version;
    }
}

This My Interface Class for Splash Response and Request

 @POST("authenticate/")
    Call<SplashResponse> splashScreen(@Body SplashRequest splashRequest);

The model class Splash Response was wrong, I made changes to it and its working fine.

public class SplashResponse implements Serializable {

        @SerializedName("success")
        @Expose
        private Boolean success;
        @SerializedName("extras")
        @Expose
        private Extras extras;

        public Boolean getSuccess() {
            return success;
        }

        public void setSuccess(Boolean success) {
            this.success = success;
        }

        public Extras getExtras() {
            return extras;
        }

        public void setExtras(Extras extras) {
            this.extras = extras;
        }


    public class Extras {

        @SerializedName("Status")
        @Expose
        private String status;
        @SerializedName("Env_Type")
        @Expose
        private Integer envType;
        @SerializedName("ApiKey")
        @Expose
        private String apiKey;
        @SerializedName("Whether_Latest_Version")
        @Expose
        private Boolean whetherLatestVersion;
        @SerializedName("SocketIO_Data")
        @Expose
        private SocketIOData socketIOData;

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }

        public Integer getEnvType() {
            return envType;
        }

        public void setEnvType(Integer envType) {
            this.envType = envType;
        }

        public String getApiKey() {
            return apiKey;
        }

        public void setApiKey(String apiKey) {
            this.apiKey = apiKey;
        }

        public Boolean getWhetherLatestVersion() {
            return whetherLatestVersion;
        }

        public void setWhetherLatestVersion(Boolean whetherLatestVersion) {
            this.whetherLatestVersion = whetherLatestVersion;
        }

        public SocketIOData getSocketIOData() {
            return socketIOData;
        }

        public void setSocketIOData(SocketIOData socketIOData) {
            this.socketIOData = socketIOData;
        }
        @SerializedName("code")
        @Expose
        private Integer code;
        @SerializedName("msg")
        @Expose
        private String msg;

        public Integer getCode() {
            return code;
        }

        public void setCode(Integer code) {
            this.code = code;
        }

        public String getMsg() {
            return msg;
        }

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

    }

    public class SocketIOData {

        @SerializedName("socket_host")
        @Expose
        private String socketHost;
        @SerializedName("socketjs_link")
        @Expose
        private String socketjsLink;

        public String getSocketHost() {
            return socketHost;
        }

        public void setSocketHost(String socketHost) {
            this.socketHost = socketHost;
        }

        public String getSocketjsLink() {
            return socketjsLink;
        }

        public void setSocketjsLink(String socketjsLink) {
            this.socketjsLink = socketjsLink;
        }

    }


}

Next In My Main Activity I made these changes

public void SplashScreen(SplashRequest splashRequest){

    UserService userService = ApiClient.getRetrofit().create(UserService.class);

    Call<SplashResponse> splashResponseCall = userService.splashScreen(splashRequest);

    splashResponseCall.enqueue(new Callback<SplashResponse>() {
        @Override
        public void onResponse(Call<SplashResponse> call, Response<SplashResponse> response) {

            Gson gson = new Gson();
            String successResponse = gson.toJson(response.body());
            SplashResponse response1 = response.body();

            if (response.isSuccessful()) {
                Toast.makeText(MainActivity.this, response1.getExtras().getStatus(), Toast.LENGTH_LONG).show();
            }else {

                //Toast.makeText(MainActivity.this, response1.getExtras().getStatus(), Toast.LENGTH_LONG).show();

                if(response1.getExtras().getCode() == 1){

                    Toast.makeText(MainActivity.this, response1.getExtras().getMsg(), Toast.LENGTH_LONG).show();

                }else{
                    Toast.makeText(MainActivity.this, response1.getExtras().getMsg(), Toast.LENGTH_LONG).show();

                }
            }
        }

        @Override
        public void onFailure(Call<SplashResponse> call, Throwable t) {

            String msg = t.getLocalizedMessage();
            Toast.makeText(MainActivity.this,msg,Toast.LENGTH_SHORT).show();
        }
    });


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM