我正在申请存款和提款,因为我已经向API提供了参数,我可以得到响应,但是我想获得数组中某些元素的干净结果:
{
"user": {
"id": "3",
"first_name": "jef",
"last_name": "handani",
"email": "jeffryhandani@gmail.com",
"verify_status": "1",
"Store_name": "7 eleven",
"Store_City": "Kuala Lumpur",
"Store_Region": "Kuala Lumpur",
"Store_address": "Jalan Bukit Jalil",
"Latitudes": "5.9668083",
"Longtitudes": "13.0338011",
"created_by": null,
"qr_code": "307",
"first_login": "1"
},
"total_amount": "220.00",
"total_fee": "20.00",
"amount": "200",
"method": "Express Exchange / Local bank transfer"
}
所以我需要从响应中获取总金额,总费用,金额和方法。>
在下面的代码中,我只需要获取上面提到的数据,有人可以帮我吗?
private void depositVerify(){
StringRequest request = new StringRequest(Request.Method.POST, URLstring, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
TextView resp = findViewById(R.id.serverResp);
resp.setText(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
EditText amount = findViewById(R.id.editamount);
Map<String, String> param = new HashMap<>();
param.put("api_key", getApi_key());
param.put("user_key", getUser_key());
param.put("currency", "debit_base");
param.put("amount", amount.getText().toString());
param.put("method", "local_bank");
return param;
}
};
Volley.newRequestQueue(depositAlert1.this).add(request);
}
答案 0 :(得分:2)
步骤1:集成到库下方。
implementation 'com.google.code.gson:gson:2.8.5'
步骤2:在POJO类下面进行复制。
public class Example {
@SerializedName("user")
@Expose
private User user;
@SerializedName("total_amount")
@Expose
private String totalAmount;
@SerializedName("total_fee")
@Expose
private String totalFee;
@SerializedName("amount")
@Expose
private String amount;
@SerializedName("method")
@Expose
private String method;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getTotalAmount() {
return totalAmount;
}
public void setTotalAmount(String totalAmount) {
this.totalAmount = totalAmount;
}
public String getTotalFee() {
return totalFee;
}
public void setTotalFee(String totalFee) {
this.totalFee = totalFee;
}
public String getAmount() {
return amount;
}
public void setAmount(String amount) {
this.amount = amount;
}
public String getMethod() {
return method;
}
public void setMethod(String method) {
this.method = method;
}
public class User {
@SerializedName("id")
@Expose
private String id;
@SerializedName("first_name")
@Expose
private String firstName;
@SerializedName("last_name")
@Expose
private String lastName;
@SerializedName("email")
@Expose
private String email;
@SerializedName("verify_status")
@Expose
private String verifyStatus;
@SerializedName("Store_name")
@Expose
private String storeName;
@SerializedName("Store_City")
@Expose
private String storeCity;
@SerializedName("Store_Region")
@Expose
private String storeRegion;
@SerializedName("Store_address")
@Expose
private String storeAddress;
@SerializedName("Latitudes")
@Expose
private String latitudes;
@SerializedName("Longtitudes")
@Expose
private String longtitudes;
@SerializedName("created_by")
@Expose
private Object createdBy;
@SerializedName("qr_code")
@Expose
private String qrCode;
@SerializedName("first_login")
@Expose
private String firstLogin;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getVerifyStatus() {
return verifyStatus;
}
public void setVerifyStatus(String verifyStatus) {
this.verifyStatus = verifyStatus;
}
public String getStoreName() {
return storeName;
}
public void setStoreName(String storeName) {
this.storeName = storeName;
}
public String getStoreCity() {
return storeCity;
}
public void setStoreCity(String storeCity) {
this.storeCity = storeCity;
}
public String getStoreRegion() {
return storeRegion;
}
public void setStoreRegion(String storeRegion) {
this.storeRegion = storeRegion;
}
public String getStoreAddress() {
return storeAddress;
}
public void setStoreAddress(String storeAddress) {
this.storeAddress = storeAddress;
}
public String getLatitudes() {
return latitudes;
}
public void setLatitudes(String latitudes) {
this.latitudes = latitudes;
}
public String getLongtitudes() {
return longtitudes;
}
public void setLongtitudes(String longtitudes) {
this.longtitudes = longtitudes;
}
public Object getCreatedBy() {
return createdBy;
}
public void setCreatedBy(Object createdBy) {
this.createdBy = createdBy;
}
public String getQrCode() {
return qrCode;
}
public void setQrCode(String qrCode) {
this.qrCode = qrCode;
}
public String getFirstLogin() {
return firstLogin;
}
public void setFirstLogin(String firstLogin) {
this.firstLogin = firstLogin;
}
}
}
第3步:解析响应。
private void depositVerify(){
StringRequest request = new StringRequest(Request.Method.POST, URLstring, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Example example = new Gson().fromJson(response,Example.class);
String total_amount = example.getTotalAmount();
String total_fee = example.getTotalFee();
String amount = example.getAmount();
String method = example.getMethod();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
EditText amount = findViewById(R.id.editamount);
Map<String, String> param = new HashMap<>();
param.put("api_key", getApi_key());
param.put("user_key", getUser_key());
param.put("currency", "debit_base");
param.put("amount", amount.getText().toString());
param.put("method", "local_bank");
return param;
}
};
Volley.newRequestQueue(depositAlert1.this).add(request);
}
答案 1 :(得分:1)
您可以按以下方式评估JSON。试试这个。
try{
JSONObject jsonObject = new JSONObject(reponse);
String total_amount = jsonObject.getString("total_amount");
String total_fee = jsonObject.getString("total_fee");
String amount = jsonObject.getString("amount");
String method = jsonObject.getString("method");
DebugLog.e("total_amount " + jsonObject.get("total_amount"));
DebugLog.e("total_amount text " + jsonObject.getString("total_amount"));
} catch(Exception ex){
ex.printStackTrace();
}
output : E/MainActivity.java: [test:77]total_amount 220.00
E/MainActivity.java: [test:78]total_amount text 220.00
答案 2 :(得分:1)
您应该尝试使用这种凌空代码来获取数据。
private void depositVerify(){
StringRequest stringRequest = new StringRequest(Request.Method.POST, URLstring, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject object = new JSONObject(response);
String total_amount = object.getString("total_amount");
String total_fee = object.getString("total_fee");
String amount = object.getString("amount");
String method = object.getString("method");
// If you want to fetch "user" object data then you can fetch like this.
JSONObject obj = object.getJSONObject("user");
String id = obj.getString("id");
String first_name = obj.getString("first_name");
String last_name = obj.getString("last_name");
String email = obj.getString("email");
String verify_status = obj.getString("verify_status");
String store_name = obj.getString("Store_name");
String store_City = obj.getString("Store_City");
String store_Region = obj.getString("Store_Region");
String store_address = obj.getString("Store_address");
String latitudes = obj.getString("Latitudes");
String longtitudes = obj.getString("Longtitudes");
String created_by = obj.getString("created_by");
String qr_code = obj.getString("qr_code");
String first_login = obj.getString("first_login");
// Then either you can set in textview or edittext or you can add in bean class
edt.setText(total_amount); //Like this you can set in edittext/textview
MyBean bean = new MyBean();
bean.setTotalAmount(total_amount); // Like this you can set in bean class
arrayList.add(bean); // Add bean in arraylist then set adapter.
}catch (Exception e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
EditText amount = findViewById(R.id.editamount);
Map<String, String> param = new HashMap<>();
param.put("api_key", getApi_key());
param.put("user_key", getUser_key());
param.put("currency", "debit_base");
param.put("amount", amount.getText().toString());
param.put("method", "local_bank");
return param;
}
};
RequestQueue queue = Volley.newRequestQueue(depositAlert1.this);
stringRequest.setRetryPolicy(new DefaultRetryPolicy(60000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
queue.add(stringRequest);
}