简体   繁体   English

响应不是来自使用改造 2 的服务器

[英]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.嗨,在下面的代码中,我有一个 get 方法,它来自使用改造库解析 json 响应的 get 方法。

For the below challenge class parsing the json response through pojo classes and but response is not coming from server .对于下面的挑战类,通过 pojo 类解析 json 响应,但响应不是来自 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:获取挑战.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:改造实例.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:获取通知数据服务.java:

 public interface GetNoticeDataService {

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

ManageChallenge.java:管理挑战.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: 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}}

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 GetChallengeList 类

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

In your postman response, "result" return a json object, but in your ManageChallenge.java.在您的邮递员回复中,“结果”返回一个 json 对象,但在您的 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. PS2。 Sorry for my terrible English skill.对不起,我糟糕的英语水平。

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 .我不明白您正在使用改造并使用 json 对象获取响应,您可以直接将模型调用传递到改造中,而不是使用模型调用轻松获取数据。 responce.body().challenge().getResult().toString()响应.body().challenge().getResult().toString()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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