Модифицированный ответ - PullRequest
       1

Модифицированный ответ

0 голосов
/ 15 ноября 2018

Итак, у меня есть этот JSON-ответ от сервера:

{
    "result": {
        "id": 30,
        "status": "Successful."
    }
}

И класс Java, где:

public class JSONResponse {
    @SerializedName("result")
    public JsonObject res;
    @SerializedName("id")
    public int id;
    @SerializedName("status")
    public String msg;
}

А вот где я звоню в службу:

customerResponseCall.enqueue(new Callback<CustomerRequestResponse>() {
            @Override
            public void onResponse(Call<CustomerRequestResponse> call, Response<CustomerRequestResponse> response) {
               response.body().res.get(String.valueOf(response.body().id));
                Toast.makeText(MainActivity.this, "User Registed Successfully!!!" + "\n" + "User ID = " + response.body().id, Toast.LENGTH_LONG).show();// this  your result

            }

            @Override
            public void onFailure(Call<CustomerRequestResponse> call, Throwable t) {
                Log.e("response-failure", call.toString());
            }
        });

И я хочу иметь возможность получить значение идентификатора, когда есть ответ от сервера. Как мне это сделать? Пожалуйста, помогите

1 Ответ

0 голосов
/ 15 ноября 2018

Измените свой JSONResponse, как показано ниже;потому что JSON, который вы получаете, имеет JSONObject result

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class CustomerRequestResponse{

@SerializedName("result")
@Expose
private Result result;

public Result getResult() {
return result;
}

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

}

Результат класса

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Result {

@SerializedName("id")
@Expose
private Integer id;
@SerializedName("status")
@Expose
private String status;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getStatus() {
return status;
}

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

}

Измените код на

customerResponseCall.enqueue(new Callback<CustomerRequestResponse>() {
            @Override
            public void onResponse(Call<CustomerRequestResponse> call, Response<CustomerRequestResponse> response) {
                Integer id =  response.body().getResult().getId();
                Toast.makeText(MainActivity.this, "User Registered Successfully!!!" + "\n" + "User ID = " + id, Toast.LENGTH_LONG).show();// this  your result

            }

            @Override
            public void onFailure(Call<CustomerRequestResponse> call, Throwable t) {
                Log.e("response-failure", call.toString());
            }
        });
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...