想要在 retrofitV2 中设置字符串或 TextView 中的 ArrayList 名称
Posted
技术标签:
【中文标题】想要在 retrofitV2 中设置字符串或 TextView 中的 ArrayList 名称【英文标题】:Want to set the ArrayList name in string or TextView in retrofitV2 【发布时间】:2019-08-26 05:17:25 【问题描述】:我有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"
我想在retrofitV2中将响应中的“message”和“message2”设置为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;
这是我已经完成的代码
【问题讨论】:
提供您的代码 我尝试了很多次,但我不明白我应该使用什么 - Yuri Popiv 至少我需要 URL 和参数来为您提供一些示例代码 你能帮我写代码吗,我应该如何制作 ArrayList 以及如何使用改造在 TextView 中设置 ArrayList 键名,问题中的 GET 响应是相同的响应,只是值不同 -尤里波波夫 当然,但是请提供您的 API 请求、活动代码等代码。 【参考方案1】:因此,如果您无法更改对"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());
【讨论】:
是否可以在不改变Api响应的情况下在Object中进行 所以我必须创建 JSONObject 类而不是创建 ArrayList?。 我想在 textview 中设置 JSONObject 的名称而不是 JSONObject 中的值,我可以在 for 循环中运行它吗? @YuriPopiv @AviSoni 我更新了答案。但是您不能在循环中运行这样的响应,因为"suggestion"
是 object
而不是 array
是否有任何其他选项可以循环运行,因为建议的响应总是不同,大小也会不同@YuriPopiv以上是关于想要在 retrofitV2 中设置字符串或 TextView 中的 ArrayList 名称的主要内容,如果未能解决你的问题,请参考以下文章