jyothi gen
jyothi gen

Reputation: 29

Response is not coming from server using retrofit2

Hi in the below code i have a get method from get method parsing the json response using retrofit library.

For the below challenge class parsing the json response through pojo classes and but response is not coming from server .

can any one please help to resolve this issue response :

{
        success: true,
        result: {
                token: TOKENSTRING,    // Challenge token to be used for login.
                serverTime: TIMESTAMP, // Current Server time
                expireTime: TIMESTAMP  // Time when token expires
        }
}

GetChallenge.java:

private void getchallenge() {


        //Here a logging interceptor is created
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);

        //The logging interceptor will be added to the http client
        OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
        httpClient.addInterceptor(logging);

        final GetNoticeDataService service = RetrofitInstance.getRetrofitInstance().create(GetNoticeDataService.class);

        /** Call the method with parameter in the interface to get the notice data*/
        Call<ManageChallenge> call = service.getChallengeList();

        /**Log the URL called*/
        Log.wtf("URL Called", call.request().url() + "");

        call.enqueue(new Callback<ManageChallenge>() {
            @Override
            public void onResponse(Call<ManageChallenge> call, Response<ManageChallenge> response) {
                if(response.isSuccessful() ) {

                    ManageChallenge challenge=response.body();

                   // String response1=response.body().toString();
                    String success=challenge.getSuccess().toString();

                    if(success.equals("true")){
                        String result= challenge.getResult().toString();

                        try {
                            JSONObject jsonObject =new JSONObject(result);

                            String token = jsonObject.getString("token");
                            Log.i("token", "token" + token);
                            String serverTime =jsonObject.getString("serverTime");
                            Log.i("serverTime", "serverTime" + serverTime);
                            String expireTime =jsonObject.getString("expireTime");
                            Log.i("expireTime", "expireTime" + expireTime);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    Log.i("REsult", "Get submitted to API." + challenge);

                }
            }

            @Override
            public void onFailure(Call<ManageChallenge> call, Throwable t) {
                Toast.makeText(LoginActivity.this, "Something went wrong...Error message: " + t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
}

RetrofitInstance.java:

public class RetrofitInstance {

    private static Retrofit retrofit;
    private static final String BASE_URL = "http://XXXXXXXXXXXX/";

    /**
     * Create an instance of Retrofit object
     * */
    public static Retrofit getRetrofitInstance() {
        if (retrofit == null) {
            retrofit = new retrofit2.Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }
}

GetNoticeDataService.java:

 public interface GetNoticeDataService {

        @Headers("Content-Type: application/json")
        @GET("webservice.php?operation=getchallenge&username=admin")
        Call<ManageChallenge> getChallengeList();
    }

ManageChallenge.java:

public class ManageChallenge {

    @SerializedName("success")
    private String success;

    @SerializedName("result")
    private List <getChallengeList> result;

    public String getSuccess() {
        return success;
    }

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

    public List<getChallengeList> getResult() {
        return result;
    }

    public void setResult(List<getChallengeList> result) {
        this.result = result;
    }
}

getChallengeList.java:

public class getChallengeList {

        @SerializedName("token")
        @Expose
        private String token;
        @SerializedName("serverTime")
        @Expose
        private String serverTime;
        @SerializedName("expireTime")
        @Expose
        private String expireTime;


        public String getToken() {
            return token;
        }

        public void setToken(String token) {
            this.token = token;
        }

        public String getServerTime() {
            return serverTime;
        }

        public void setServerTime(String serverTime) {
            this.serverTime = serverTime;
        }

        public String getExpireTime() {
            return expireTime;
        }

        public void setExpireTime(String expireTime) {
            this.expireTime = expireTime;
        }
    public getChallengeList(String tokens, String expireTimes, String serverTimes){
        token = tokens;
        expireTime = expireTimes;
        serverTime = serverTimes;
    }

}

Postman response:

{"success":true,"result":{"token":"5e2ab99eb318f","serverTime":1579858334,"expireTime":1579858634}}

Upvotes: 3

Views: 77

Answers (3)

Dulanga
Dulanga

Reputation: 931

According to your response, there is no List there for the issue is here

@SerializedName("result")
private List<GetChallengeList> result;

Change it like this

public class ManageChallenge {

    @SerializedName("success")
    private String success;

    @SerializedName("result")
    private GetChallengeList result;

    public String getSuccess() {
        return success;
    }

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

    public GetChallengeList getResult() {
        return result;
    }

    public void setResult(GetChallengeList result) {
        this.result = result;
    }
}

GetChallengeList class

public class GetChallengeList {

    @SerializedName("token")
    @Expose
    private String token;
    @SerializedName("serverTime")
    @Expose
    private String serverTime;
    @SerializedName("expireTime")
    @Expose
    private String expireTime;

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public String getServerTime() {
        return serverTime;
    }

    public void setServerTime(String serverTime) {
        this.serverTime = serverTime;
    }

    public String getExpireTime() {
        return expireTime;
    }

    public void setExpireTime(String expireTime) {
        this.expireTime = expireTime;
    }

    public GetChallengeList(String tokens, String expireTimes, String serverTimes){
        token = tokens;
        expireTime = expireTimes;
        serverTime = serverTimes;
    }
}

Upvotes: 1

Amit pandey
Amit pandey

Reputation: 1195

I dont understand this you are using retrofit and getting the response using json object, directly you can pass you model call into retrofit and than easily get the data using the model call . responce.body().challenge().getResult().toString()

Upvotes: 0

Hello World
Hello World

Reputation: 740

In your postman response, "result" return a json object, but in your ManageChallenge.java. You declare result as List of object. So I thinks that it may cause some error when casting.

PS. You should declare your class name with upper case at the 1st character. If not, it's may cause some confuse in the future.

PS2. Sorry for my terrible English skill.

Upvotes: 0

Related Questions