首页 > 解决方案 > 想要在 retrofitV2 中设置字符串或 TextView 中的 ArrayList 名称

问题描述

我有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"
        }
    }
}

我想将响应中的“消息”和“消息 2”设置为 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;
    }

}

这是我已经完成的代码

标签: androidjsonarraylistretrofit2

解决方案


因此,如果您无法更改响应,"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());

推荐阅读