Retrofit的响应无法获取对象值

时间:2018-07-12 08:21:24

标签: java android retrofit2

  

{“ StatusCode”:200,“ StatusDescription”:“ OK”,“ ErrorMessage”:   “”,“ ErrorDetail”:“”,“ Results”:[       {         “密钥”:“ AccessTokens”,         “ Value”:“ [{\” Key \“:\” XXXXX“,         \“ Value \”:\“ BABABA \”},{\“ Key \”:\“ DIDADIDA \”,\“ Value \”:\“ YYYYY”       }]“}]}

这是我成功调用API时得到的响应。 “结果”的数据类型为列表。谁能为我解释如何获取“键”和“值”。

我的对象类

public class KeyValueItem {

    private String Key;
    private String Value;

    public String getKey() {
        return Key;
    }

    public void setKey(String key) {
        Key = key;
    }

    public String getValue() {
        return Value;
    }

    public void setValue(String value) {
        Value = value;
    }
}

响应类别

public class RestServiceResponse {

    @SerializedName("StatusCode")
    @Expose
    public int StatusCode;
    public int getStatusCode() {
        return StatusCode;
    }


    @SerializedName("StatusDescription")
    @Expose
    public String StatusDescription;
    public String getStatusDescription() {
        return StatusDescription;
    }


    @SerializedName("ErrorMessage")
    @Expose
    public String ErrorMessage;
    public String getErrorMessage() {
        return ErrorMessage;
    }


    @SerializedName("ErrorDetail")
    @Expose
    public String ErrorDetail;
    public String getErrorDetail() {
        return ErrorDetail;
    }


    @SerializedName("Results")
    @Expose
    public List<KeyValueItem> Results;
    public List<KeyValueItem> getResults() {
        return Results;
    }

}

请任何人帮助=(

我的一些代码:

public void onResponse(Call<RestServiceResponse> call, Response<RestServiceResponse> response) {

                    Log.i("ddsddsadsa", String.valueOf(response.code()));
                    RestServiceResponse restServiceResponse = response.body();
                    if(restServiceResponse.getStatusCode() == 200){
                        List<KeyValueItem> list = response.body().getResults();
                        JSONArray jsonArray = new JSONArray(list);
                        try {
                            JSONObject job = jsonArray.getJSONObject(1);
                            String testttt = job.getString("Key");
                            Log.i("dsadsadsadas", testttt);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }

2 个答案:

答案 0 :(得分:2)

首先要了解的两件事。

  1. 您的JSON数据格式无效。它包含\(斜杠)以在键值对中转义双引号。要确认返回的JSON数据是否有效,请将您的JSON响应复制并粘贴到JSON validator and Formatter中。服务器脚本中可能有问题。
  2. 如果您将GsonConvertorFactory与Retrofit一起使用,Retrofit将自动在内部将JSON响应数据转换为POJO。因此,您不需要在onResponse()方法中再次解析它。如果您从服务器端获得了正确的JSON响应,请按如下所示使用它。

    public void onResponse(Call<RestServiceResponse> call, Response<RestServiceResponse> response) {
    
                // code....
                RestServiceResponse restServiceResponse = response.body();
    
                if (restServiceResponse.getStatusCode() == 200) {
    
                    List<KeyValueItem> list = response.body().getResults();
    
                    for(int i = 0; i < list.size(); i++) {
                         KeyValueItem kvi = list.get(i);
                         // do whatever you want with kvi object
                    }
                }
    }
    

答案 1 :(得分:1)

public void onResponse(Call<RestServiceResponse> call, Response<RestServiceResponse> response) {

                    Log.i("ddsddsadsa", String.valueOf(response.code()));
                    RestServiceResponse restServiceResponse = response.body();
                    if(restServiceResponse.getStatusCode() == 200){


                        List<KeyValueItem> list = response.body().getResults();
                        for(KeyValueItem keyValueItem : list) {
                            String key = keyValueItem.getKey();
                            String value = keyValueItem.getValue();

                            Log.i("Keykeykey", key);
                        }

                            try {
                                JSONArray jsonArray = new JSONArray(value);
                                for(int i = 0; i <  jsonArray.length();i++) {
                                    JSONObject obj = jsonArray.getJSONObject(i);
                                    String keykey = obj.getString("Key");
                                    String VAlll = obj.getString("Value");
                                    Log.i("c1111",keykey);
                                    Log.i("c222222", VAlll);
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }


                    }else if(restServiceResponse.getErrorMessage() != null){
                        builder =  new AlertDialog.Builder(LoginActivity.this);
                        builder.setTitle("Error");
                        builder.setMessage(restServiceResponse.getErrorMessage());
                        builder.setPositiveButton("Ok",null);
                        AlertDialog alertDialog = builder.create();
                        alertDialog.show();
                    }
                }

好。顺便说一句。我已经尝试过这个来得到我的结果。而且有效! 要回答有关无效JSON格式的问题,可能是因为我更改了JSON的值,所以可能对此有一些错误。

以下是我得到的最终日志:

  

74 / com.appandus.user.konnect I / Keykeykey:AccessTokens 07-12   17:14:38.177 6274-6274 / com.appandus.user.konnect I / c1111:XXXXX 07-12   17:14:38.177 6274-6274 / com.appandus.user.konnect I / c222222:BABABA   07-12 17:14:38.177 6274-6274 / com.appandus.user.konnect I / c1111:NS / NH   :DIDAIDA 07-12 17:14:38.177 6274-6274 / com.appandus.user.konnect   I / c222222:YYYYYY