想要在字符串中设置ArrayList名称,或在retrofitV2中设置TextView

时间:2019-04-04 12:35:14

标签: android json arraylist retrofit2

我有json响应

{
    "status": "success",
    "suggestion": {
        "message": {
            "message_a":"a",
            "message_b":"b"
        },
        "message1": {
            "message_a":"a",
            "message_b":"b"
        },
        "message2": {
            "message_a":"a",
            "message_b":"b"
        }
    }
}

我想将响应中的“ message”和“ message2”设置为RetrofitV2中的String或TextView

ApiInterface类

public interface ApiInterface {
@GET(“url”)
Call<Contributor>getCheck();
}

贡献者

public class Contributor {
    @SerializedName("status")
    String status;
    @SerializedName("suggestion")
    ArrayList<Suggestion_List>suggestion;

    public String getName() {
            return name;
    }

    public ArrayList<Suggestion_List> getSuggestion() {
        return suggestion;
    }

}

这是我仍然做过的代码

1 个答案:

答案 0 :(得分:0)

因此,如果您无法更改对"message": {},"message": {},"message": {}的响应,则将其解析为JSONObject是唯一的方法。

首先创建:

    public class Message {

        @SerializedName("message_a")
        private String message_a;
        @SerializedName("message_b")
        private String message_b;

    public Message(String message_a, String message_b) {
        this.message_a = message_a;
        this.message_b = message_b;
    }

        public String getMessage_a() {
            return message_a;
        }

        public void setMessage_a(String message_a) {
            this.message_a = message_a;
        }

        public String getMessage_b() {
            return message_b;
        }

        public void setMessage_b(String message_b) {
            this.message_b = message_b;
        }
    }

}

public interface ApiInterface {
    @GET("url")
    Call<JSONObject> getCheck();
}

最后一次活动

private ApiInterface apiInterface;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    apiInterface = ApiClient.getApiClient().create(ApiInterface.class);

    Call<JSONObject> call = apiInterface.getCheck();
    call.enqueue(new Callback<JSONObject>() {
        @Override
        public void onResponse(Call<JSONObject> call, Response<JSONObject> response) {
            try {
                JSONObject suggestion = response.body().getJSONObject("suggestion");
                Message message = new Message(suggestion.getJSONObject("message").getString("message_a"), 
                        suggestion.getJSONObject("message").getString("message_b"));
                Message message1 = new Message(suggestion.getJSONObject("message1").getString("message_a"), 
                        suggestion.getJSONObject("message1").getString("message_b"));
                Message message2 = new Message(suggestion.getJSONObject("message2").getString("message_a"), 
                        suggestion.getJSONObject("message2").getString("message_b"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

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

        }
    });


}

然后将文本设置为TextView

textView.setText(message.getMessage_a());
textView.setText(message.getMessage_b());