嵌套 JSON 对象(改造)
Nested JSON Object (Retrofit)
我正在使用 Retrofit 发送图片请求并接收此 Json
{"response":{
"face":{
"value":"true",
"confidence":55
},
"gender":{
"value":"male",
"confidence":73
},
...
}}
并且我通过 Retrofit 收到它......
RestAdapter adapter = new RestAdapter.Builder()
.setLogLevel(RestAdapter.LogLevel.FULL)
.setEndpoint(END_POINT)
.build();
Mylistenerr listener = adapter.create(Mylistenerr.class);
File photo = new File(picturePath);
TypedFile image = new TypedFile("multipart/image/jpg", photo);
listener.setUserImage(
image,
new Callback<respostring>() {
@Override
public void success(respostring rp, Response arg1) {}
@Override
public void failure(RetrofitError arg0) {
pd.hide();
pd.dismiss();
Log.d("ERROR:", arg0.getLocalizedMessage());
}
});
}
private static class respostring {
private Content face;
private Content gender;
respostring() {}
}
class Content
{
private int confidence;
private String value;
Content(){}
public int getconf(){
return this.confidence;
}
public String getvalue(){
return this.value;
}
}
我的界面
public interface Mylistenerr {
@Multipart
@POST("/public/test")
void setUserImage(
@Part("image") TypedFile file,
Callback<respostring> response);
}
但是有改装错误。这里有什么我想念的吗?
我建议您改用 Gson 进行 json 反序列化,因为改造对它的支持非常好。然后你可以像这样创建 classes:
你的脸class:
public class Face implements Serializable {
@SerializedName("value")
public boolean value;
@SerializedName("confidence")
public int confidence;
}
您的性别class:
public class Gender implements Serializable {
@SerializedName("value")
public String value;
@SerializedName("confidence")
public int confidence;
}
您的回复class:
public class YourResponseType implements Serializable {
@SerializedName("face")
public Face face;
@SerializedName("gender")
public Gender gender;
}
然后您实际上可以进行改造,为您完成剩下的工作:
listener.setUserImage(image, new Callback<YourResonseType>() {...});
希望对您有所帮助!
我正在使用 Retrofit 发送图片请求并接收此 Json
{"response":{
"face":{
"value":"true",
"confidence":55
},
"gender":{
"value":"male",
"confidence":73
},
...
}}
并且我通过 Retrofit 收到它......
RestAdapter adapter = new RestAdapter.Builder()
.setLogLevel(RestAdapter.LogLevel.FULL)
.setEndpoint(END_POINT)
.build();
Mylistenerr listener = adapter.create(Mylistenerr.class);
File photo = new File(picturePath);
TypedFile image = new TypedFile("multipart/image/jpg", photo);
listener.setUserImage(
image,
new Callback<respostring>() {
@Override
public void success(respostring rp, Response arg1) {}
@Override
public void failure(RetrofitError arg0) {
pd.hide();
pd.dismiss();
Log.d("ERROR:", arg0.getLocalizedMessage());
}
});
}
private static class respostring {
private Content face;
private Content gender;
respostring() {}
}
class Content
{
private int confidence;
private String value;
Content(){}
public int getconf(){
return this.confidence;
}
public String getvalue(){
return this.value;
}
}
我的界面
public interface Mylistenerr {
@Multipart
@POST("/public/test")
void setUserImage(
@Part("image") TypedFile file,
Callback<respostring> response);
}
但是有改装错误。这里有什么我想念的吗?
我建议您改用 Gson 进行 json 反序列化,因为改造对它的支持非常好。然后你可以像这样创建 classes:
你的脸class:
public class Face implements Serializable {
@SerializedName("value")
public boolean value;
@SerializedName("confidence")
public int confidence;
}
您的性别class:
public class Gender implements Serializable {
@SerializedName("value")
public String value;
@SerializedName("confidence")
public int confidence;
}
您的回复class:
public class YourResponseType implements Serializable {
@SerializedName("face")
public Face face;
@SerializedName("gender")
public Gender gender;
}
然后您实际上可以进行改造,为您完成剩下的工作:
listener.setUserImage(image, new Callback<YourResonseType>() {...});
希望对您有所帮助!