[英]Get Response in Retrofit Android
I'm new to learning ROS and retrofit.我是学习 ROS 和 retrofit 的新手。 Can anyone help me or point out why its causing me this error, basically I want to get the name and status under the hardware json.
任何人都可以帮助我或指出为什么它导致我这个错误,基本上我想在硬件json下获得名称和状态。 I'm not sure if my error is coming from my POJO model.
我不确定我的错误是否来自我的 POJO model。
In your
ObjectList`, create a getter to get string from hardware array: In your
ObjectList` 中,创建一个 getter 以从硬件数组中获取字符串:
public String getHardwareString() {
String result = "";
for (int i=0; i<hardware.length; i++) {
Object item = hardware[i];
result += item.name
}
return result;
}
Change the result
concatenation corresponding with your requirement.根据您的要求更改
result
连接。
In your fragment:在您的片段中:
textviewhardware.setText(response.body().getHardwareString());
E/ mainAction: response com.nera.rosbridgeclient.messages.robot_msgs.ObjectList@cea25bf hardware[Lcom.nera.rosbridgeclient.messages.robot_msgs.Object;@7ec748c
E/mainAction:响应 com.nera.rosbridgeclient.messages.robot_msgs.ObjectList@cea25bf 硬件[Lcom.nera.rosbridgeclient.messages.robot_msgs.Object;@7ec
This is not an Error/Exception
, it's your Log message
.这不是
Error/Exception
,而是您的Log message
。
Log.e(" mainAction", " hardware"+ response.body().getHardware().toString());
The above line generates that error logs
.上面的行生成
error logs
。
Cause:原因:
response.body().getHardware()`
response.body().getHardware()`
The above line will return
you an array
of Hardware
.上面的行将
return
一个Hardware
array
。 Means an instance/object
of a hardware array
.表示
hardware array
的instance/object
。
So when you use toString()
to an instance/object
like因此,当您将
toString()
用于instance/object
时
response.body().getHardware().toString()
response.body().getHardware().toString()
It will give you the instance
as String
.它将为您提供
String
的instance
。
Solution:解决方案:
You are getting an Array
of Hardware
in response.您将得到一个
Hardware
Array
作为响应。 So you have to iterate it like the following.所以你必须像下面这样迭代它。
@Override
public void onResponse(Call<ObjectList> call, Response<ObjectList> response) {
StringBuilder sb = new StringBuilder(); // add this line
for(int i=0; i<response.body().getHardware().length; i++){
Log.e(" mainAction", " hardware"+ response.body().getHardware()[i].name);
sb.append(response.body().getHardware()[i].name)); // here we append each hardware name to our string builder.
sb.append(" "); // here we append a space separator after every hardware name, you can use whatever you want(like: newline[\n] or coma[,]).
}
textviewhardware.setText(sb.toString()); // now textview will show every hardware name.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.