gpt4 book ai didi

java - 迭代 JSON 响应的结果 - Android + Retrofit + Moshi

转载 作者:行者123 更新时间:2023-12-02 09:03:53 26 4
gpt4 key购买 nike

{
"data": [
{
"id": 4,
"customer_id": 3,
"service_type_id": 1,
"full_name": "Teja Babu S",
"email": "testemail@gmail.com",
camera_types": 1,
"dvr_types": 1,
"created_at": "2020-01-04 14:18:30",
"updated_at": "2020-01-04 14:18:30",
"camera_type": {
"id": 1,
"name": "Analogue hd",
"description": null,
"status": 1,
"deleted_at": null,
"created_at": "2020-01-04 08:03:45",
"updated_at": "2020-01-04 08:54:23"
},
"dvr_type": {
"id": 1,
"name": "XVR - nvr",
"description": "desc",
"status": 1,
"deleted_at": null,
"created_at": "2020-01-04 08:28:04",
"updated_at": "2020-01-04 08:57:17"
}
}
]
}

我正在使用http://www.jsonschema2pojo.org/用于转换。为了使问题简单,我不会粘贴结果。

package - package com.tesmachino.saycure.entities.OrderHistory.OrderDetail;

ClassName - OrderDetailsResponse

Target language: Java

Source type: JSON

Annotation style: Moshi

我想从camera_type和dvr_type访问名称

我正在使用 Moshi + 改造

订单详情

package com.tesmachino.saycure;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.tesmachino.saycure.Auth.TokenManager;
import com.tesmachino.saycure.entities.OrderHistory.OrderDetail.OrderDetailsResponse;
import com.tesmachino.saycure.entities.OrderHistory.OrderHistoryResponse;
import com.tesmachino.saycure.entities.UserDetails.UserDetailsGetResponse;
import com.tesmachino.saycure.network.ApiService;
import com.tesmachino.saycure.network.RetrofitBuilder;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class OrderDetail extends AppCompatActivity {

ApiService service;
TokenManager tokenManager;

Call<OrderDetailsResponse> call;


private static final String TAG = "OrderDetail";

@BindView(R.id.orderdetails_id)
TextView orderId;

@BindView(R.id.orderdetails_client_name)
TextView orderDetailsClientName;



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order_detail);

ButterKnife.bind(this);

tokenManager = TokenManager.getInstance(getSharedPreferences("prefs", MODE_PRIVATE));
service = RetrofitBuilder.createServiceWithAuth(ApiService.class, tokenManager);
}


@Override
protected void onResume() {


//Get the Data from the Intent
if (getIntent().hasExtra("order_id")) {
int order_id = getIntent().getIntExtra("order_id", 1);
Log.d(TAG, "IntentWorking" + order_id);

call = service.orderDetails(order_id);


call.enqueue(new Callback<OrderDetailsResponse>() {
@Override
public void onResponse(Call<OrderDetailsResponse> call, Response<OrderDetailsResponse> response) {
OrderDetailsResponse orderDetails = response.body();

if (orderDetails != null){

}

Log.w(TAG, "onResponse123: " + orderDetails.getData().get(0).getId());

Toast.makeText(OrderDetail.this, "" + response.body().toString(), Toast.LENGTH_SHORT).show();
orderId.setText(String.valueOf(orderDetails.getData().get(0).getId()));
orderDetailsClientName.setText(orderDetails.getData().get(0).getFullName());
Log.w(TAG, "onResponse45321: "+ orderDetails.getData().get(0).getCameraType());

}

@Override
public void onFailure(Call<OrderDetailsResponse> call, Throwable t) {
Log.w(TAG, "onFailure: " + t.getMessage());
Toast.makeText(OrderDetail.this, "Failure" + t.getMessage(), Toast.LENGTH_SHORT).show();
}
});

} else {

Toast.makeText(this, "There seems to be an error while fetching the Order Id. Please Try Again", Toast.LENGTH_SHORT).show();
}

super.onResume();
}
}

订单详细信息响应

package com.tesmachino.saycure.entities.OrderHistory.OrderDetail;

import com.squareup.moshi.Json;

import org.apache.commons.lang3.builder.ToStringBuilder;

import java.util.List;

public class OrderDetailsResponse {

@Json(name = "data")
private List<OrderDetailsGet> data = null;

public List<OrderDetailsGet> getData() {
return data;
}

public void setData(List<OrderDetailsGet> data) {
this.data = data;
}

@Override
public String toString() {
return new ToStringBuilder(this).append("data", data).toString();
}
}

订单详情获取

package com.tesmachino.saycure.entities.OrderHistory.OrderDetail;

import com.squareup.moshi.Json;

import org.apache.commons.lang3.builder.ToStringBuilder;

public class OrderDetailsGet {

@Json(name = "id")
private Integer id;
@Json(name = "customer_id")
private Integer customerId;
@Json(name = "service_type_id")
private Integer serviceTypeId;
@Json(name = "full_name")
private String fullName;
@Json(name = "email")
private String email;
@Json(name = "address_line_1")
private String addressLine1;
@Json(name = "address_line_2")
private String addressLine2;
@Json(name = "phone_no")
private String phoneNo;
@Json(name = "alternate_phone_no")
private Object alternatePhoneNo;
@Json(name = "land_mark")
private Object landMark;
@Json(name = "area")
private Object area;
@Json(name = "district")
private String district;
@Json(name = "city")
private String city;
@Json(name = "state")
private String state;
@Json(name = "pincode")
private Integer pincode;
@Json(name = "type_of_property")
private Integer typeOfProperty;
@Json(name = "camera_types")
private Integer cameraTypes;
@Json(name = "no_of_cameras")
private Integer noOfCameras;
@Json(name = "dvr_types")
private Integer dvrTypes;
@Json(name = "dvr_channel")
private Integer dvrChannel;
@Json(name = "notes")
private Object notes;
@Json(name = "deleted_at")
private Object deletedAt;
@Json(name = "created_at")
private String createdAt;
@Json(name = "updated_at")
private String updatedAt;
@Json(name = "camera_type")
private CameraType cameraType;
@Json(name = "dvr_type")
private DvrType dvrType;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public Integer getCustomerId() {
return customerId;
}

public void setCustomerId(Integer customerId) {
this.customerId = customerId;
}

public Integer getServiceTypeId() {
return serviceTypeId;
}

public void setServiceTypeId(Integer serviceTypeId) {
this.serviceTypeId = serviceTypeId;
}

public String getFullName() {
return fullName;
}

public void setFullName(String fullName) {
this.fullName = fullName;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getAddressLine1() {
return addressLine1;
}

public void setAddressLine1(String addressLine1) {
this.addressLine1 = addressLine1;
}

public String getAddressLine2() {
return addressLine2;
}

public void setAddressLine2(String addressLine2) {
this.addressLine2 = addressLine2;
}

public String getPhoneNo() {
return phoneNo;
}

public void setPhoneNo(String phoneNo) {
this.phoneNo = phoneNo;
}

public Object getAlternatePhoneNo() {
return alternatePhoneNo;
}

public void setAlternatePhoneNo(Object alternatePhoneNo) {
this.alternatePhoneNo = alternatePhoneNo;
}

public Object getLandMark() {
return landMark;
}

public void setLandMark(Object landMark) {
this.landMark = landMark;
}

public Object getArea() {
return area;
}

public void setArea(Object area) {
this.area = area;
}

public String getDistrict() {
return district;
}

public void setDistrict(String district) {
this.district = district;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

public String getState() {
return state;
}

public void setState(String state) {
this.state = state;
}

public Integer getPincode() {
return pincode;
}

public void setPincode(Integer pincode) {
this.pincode = pincode;
}

public Integer getTypeOfProperty() {
return typeOfProperty;
}

public void setTypeOfProperty(Integer typeOfProperty) {
this.typeOfProperty = typeOfProperty;
}

public Integer getCameraTypes() {
return cameraTypes;
}

public void setCameraTypes(Integer cameraTypes) {
this.cameraTypes = cameraTypes;
}

public Integer getNoOfCameras() {
return noOfCameras;
}

public void setNoOfCameras(Integer noOfCameras) {
this.noOfCameras = noOfCameras;
}

public Integer getDvrTypes() {
return dvrTypes;
}

public void setDvrTypes(Integer dvrTypes) {
this.dvrTypes = dvrTypes;
}

public Integer getDvrChannel() {
return dvrChannel;
}

public void setDvrChannel(Integer dvrChannel) {
this.dvrChannel = dvrChannel;
}

public Object getNotes() {
return notes;
}

public void setNotes(Object notes) {
this.notes = notes;
}

public Object getDeletedAt() {
return deletedAt;
}

public void setDeletedAt(Object deletedAt) {
this.deletedAt = deletedAt;
}

public String getCreatedAt() {
return createdAt;
}

public void setCreatedAt(String createdAt) {
this.createdAt = createdAt;
}

public String getUpdatedAt() {
return updatedAt;
}

public void setUpdatedAt(String updatedAt) {
this.updatedAt = updatedAt;
}

public CameraType getCameraType() {
return cameraType;
}

public void setCameraType(CameraType cameraType) {
this.cameraType = cameraType;
}

public DvrType getDvrType() {
return dvrType;
}

public void setDvrType(DvrType dvrType) {
this.dvrType = dvrType;
}

@Override
public String toString() {
return new ToStringBuilder(this).append("id", id).append("customerId", customerId).append("serviceTypeId", serviceTypeId).append("fullName", fullName).append("email", email).append("addressLine1", addressLine1).append("addressLine2", addressLine2).append("phoneNo", phoneNo).append("alternatePhoneNo", alternatePhoneNo).append("landMark", landMark).append("area", area).append("district", district).append("city", city).append("state", state).append("pincode", pincode).append("typeOfProperty", typeOfProperty).append("cameraTypes", cameraTypes).append("noOfCameras", noOfCameras).append("dvrTypes", dvrTypes).append("dvrChannel", dvrChannel).append("notes", notes).append("deletedAt", deletedAt).append("createdAt", createdAt).append("updatedAt", updatedAt).append("cameraType", cameraType).append("dvrType", dvrType).toString();
}


}

Unable to iterate JSON

最佳答案

如果您只是尝试从 camera_typedvr_type 获取名称,您可以执行以下操作:

//This assumes that you have a getCameraType & getDvrTypes method in your OrderDetailsGet class
String camera_name = orderDetails.getData().get(0).getCameraType().getName()
String dvr_name = orderDetails.getData().get(0).getDvrTypes().getName()

关于java - 迭代 JSON 响应的结果 - Android + Retrofit + Moshi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59971908/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com