如何使用 Android 中的 volley 检索 json 到 api 中数组的子数组?
How to retrieve sub-array of an array in json through api using volley in Android?
JSON api 文件:
{
"state": "Jammu and Kashmir",
"statecode": "JK",
"districtData": [
{
"district": "Anantnag",
"notes": "",
"active": 107,
"confirmed": 109,
"deceased": 1,
"recovered": 1,
"delta": {
"confirmed": 0,
"deceased": 0,
"recovered": 0
}
},
{
"district": "Budgam",
"notes": "",
"active": 18,
"confirmed": 30,
"deceased": 0,
"recovered": 12,
"delta": {
"confirmed": 4,
"deceased": 0,
"recovered": 0
}
}
]
}
Java代码:
StringRequest stringRequest = new StringRequest(Request.Method.GET,
"https://api.covid19india.org/v2/state_district_wise.json",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("districtData");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
// Need to get data here !!!
}
progressDialog.cancel();
} catch (Exception e) {
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
Logcat(调试): 下面是我在尝试 运行 我的应用程序时得到的 logcat。
2020-05-05 16:21:49.663 8813-8813/? I/owais.wabah202: Late-enabling -Xcheck:jni
2020-05-05 16:21:50.013 8813-8813/com.johnowais.wabah2020 I/Perf: Connecting to perf service.
2020-05-05 16:21:50.160 8813-8813/com.johnowais.wabah2020 W/owais.wabah202: Accessing hidden method Landroid/graphics/drawable/Drawable;->getOpticalInsets()Landroid/graphics/Insets; (light greylist, linking)
2020-05-05 16:21:50.160 8813-8813/com.johnowais.wabah2020 W/owais.wabah202: Accessing hidden field Landroid/graphics/Insets;->left:I (light greylist, linking)
2020-05-05 16:21:50.160 8813-8813/com.johnowais.wabah2020 W/owais.wabah202: Accessing hidden field Landroid/graphics/Insets;->right:I (light greylist, linking)
2020-05-05 16:21:50.160 8813-8813/com.johnowais.wabah2020 W/owais.wabah202: Accessing hidden field Landroid/graphics/Insets;->top:I (light greylist, linking)
2020-05-05 16:21:50.160 8813-8813/com.johnowais.wabah2020 W/owais.wabah202: Accessing hidden field Landroid/graphics/Insets;->bottom:I (light greylist, linking)
2020-05-05 16:21:50.235 8813-8813/com.johnowais.wabah2020 W/owais.wabah202: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (light greylist, reflection)
2020-05-05 16:21:50.239 8813-8813/com.johnowais.wabah2020 W/owais.wabah202: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (light greylist, reflection)
2020-05-05 16:21:50.289 8813-8813/com.johnowais.wabah2020 W/owais.wabah202: Accessing hidden method Landroid/widget/TextView;->getTextDirectionHeuristic()Landroid/text/TextDirectionHeuristic; (light greylist, linking)
2020-05-05 16:21:50.389 8813-8813/com.johnowais.wabah2020 D/OpenGLRenderer: Skia GL Pipeline
2020-05-05 16:21:50.495 8813-8856/com.johnowais.wabah2020 I/Adreno: QUALCOMM build : 2df12b3, I07da2d9908
Build Date : 10/04/18
OpenGL ES Shader Compiler Version: EV031.25.03.01
Local Branch :
Remote Branch :
Remote Branch :
Reconstruct Branch :
2020-05-05 16:21:50.495 8813-8856/com.johnowais.wabah2020 I/Adreno: Build Config : S L 6.0.7 AArch64
2020-05-05 16:21:50.495 8813-8856/com.johnowais.wabah2020 D/vndksupport: Loading /vendor/lib64/hw/gralloc.msm8953.so from current namespace instead of sphal namespace.
2020-05-05 16:21:50.502 8813-8856/com.johnowais.wabah2020 I/Adreno: PFP: 0x005ff087, ME: 0x005ff063
2020-05-05 16:21:50.509 8813-8856/com.johnowais.wabah2020 I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
2020-05-05 16:21:50.509 8813-8856/com.johnowais.wabah2020 I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0
2020-05-05 16:21:50.511 8813-8856/com.johnowais.wabah2020 I/OpenGLRenderer: Initialized EGL, version 1.4
2020-05-05 16:21:50.511 8813-8856/com.johnowais.wabah2020 D/OpenGLRenderer: Swap behavior 2
2020-05-05 16:21:50.513 8813-8858/com.johnowais.wabah2020 D/NetworkSecurityConfig: No Network Security Config specified, using platform default
2020-05-05 16:21:50.515 8813-8858/com.johnowais.wabah2020 I/DpmTcmClient: RegisterTcmMonitor from: com.android.okhttp.TcmIdleTimerMonitor
2020-05-05 16:21:50.565 8813-8856/com.johnowais.wabah2020 D/vndksupport: Loading /vendor/lib64/hw/android.hardware.graphics.mapper@2.0-impl.so from current namespace instead of sphal namespace.
2020-05-05 16:21:50.566 8813-8856/com.johnowais.wabah2020 D/vndksupport: Loading /vendor/lib64/hw/gralloc.msm8953.so from current namespace instead of sphal namespace.
我无法获取数据,因为它的格式不正确。我也想使用 where 子句,它会给我我想要的州的每个地区的数据。
API link: https://api.covid19india.org/v2/state_district_wise.json
结构
JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, "https://api.covid19india.org/v2/state_district_wise.json",null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try {
for (int i = 0; i < response.length(); i++) {
JSONObject dataOBJ = response.getJSONObject(i);
JSONArray jsonChild = dataOBJ.getJSONArray("districtData");
for (int k = 0; k < jsonChild.length(); k++) {
JSONObject obj = jsonChild.getJSONObject(k);
int active = obj.getInt("active");
String district = obj.getString("district");
JSONObject child = obj.getJSONObject("delta");
int confirmed = child.getInt("confirmed");
}
}
} catch (Exception exp) {
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(request);
试试这个方法
StringRequest stringRequest = new StringRequest(Request.Method.GET,
"https://api.covid19india.org/v2/state_district_wise.json",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "onResponse: "+response);
try {
JSONArray responseArray = new JSONArray(response);
for (int i = 0; i < responseArray.length(); i++) {
JSONObject stateObject = responseArray.getJSONObject(i);
JSONArray districtArray = stateObject.getJSONArray("districtData");
for(int j=0; j<districtArray.length(); j++){
JSONObject districtObject = districtArray.getJSONObject(j);
//do what you want to do with district object
Log.d(TAG, "onResponse: "+districtObject.toString());
}
}
}catch (JSONException e){
Log.d(TAG, "onResponse: "+e.getMessage());
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
JSON api 文件:
{
"state": "Jammu and Kashmir",
"statecode": "JK",
"districtData": [
{
"district": "Anantnag",
"notes": "",
"active": 107,
"confirmed": 109,
"deceased": 1,
"recovered": 1,
"delta": {
"confirmed": 0,
"deceased": 0,
"recovered": 0
}
},
{
"district": "Budgam",
"notes": "",
"active": 18,
"confirmed": 30,
"deceased": 0,
"recovered": 12,
"delta": {
"confirmed": 4,
"deceased": 0,
"recovered": 0
}
}
]
}
Java代码:
StringRequest stringRequest = new StringRequest(Request.Method.GET,
"https://api.covid19india.org/v2/state_district_wise.json",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("districtData");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
// Need to get data here !!!
}
progressDialog.cancel();
} catch (Exception e) {
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
Logcat(调试): 下面是我在尝试 运行 我的应用程序时得到的 logcat。
2020-05-05 16:21:49.663 8813-8813/? I/owais.wabah202: Late-enabling -Xcheck:jni
2020-05-05 16:21:50.013 8813-8813/com.johnowais.wabah2020 I/Perf: Connecting to perf service.
2020-05-05 16:21:50.160 8813-8813/com.johnowais.wabah2020 W/owais.wabah202: Accessing hidden method Landroid/graphics/drawable/Drawable;->getOpticalInsets()Landroid/graphics/Insets; (light greylist, linking)
2020-05-05 16:21:50.160 8813-8813/com.johnowais.wabah2020 W/owais.wabah202: Accessing hidden field Landroid/graphics/Insets;->left:I (light greylist, linking)
2020-05-05 16:21:50.160 8813-8813/com.johnowais.wabah2020 W/owais.wabah202: Accessing hidden field Landroid/graphics/Insets;->right:I (light greylist, linking)
2020-05-05 16:21:50.160 8813-8813/com.johnowais.wabah2020 W/owais.wabah202: Accessing hidden field Landroid/graphics/Insets;->top:I (light greylist, linking)
2020-05-05 16:21:50.160 8813-8813/com.johnowais.wabah2020 W/owais.wabah202: Accessing hidden field Landroid/graphics/Insets;->bottom:I (light greylist, linking)
2020-05-05 16:21:50.235 8813-8813/com.johnowais.wabah2020 W/owais.wabah202: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (light greylist, reflection)
2020-05-05 16:21:50.239 8813-8813/com.johnowais.wabah2020 W/owais.wabah202: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (light greylist, reflection)
2020-05-05 16:21:50.289 8813-8813/com.johnowais.wabah2020 W/owais.wabah202: Accessing hidden method Landroid/widget/TextView;->getTextDirectionHeuristic()Landroid/text/TextDirectionHeuristic; (light greylist, linking)
2020-05-05 16:21:50.389 8813-8813/com.johnowais.wabah2020 D/OpenGLRenderer: Skia GL Pipeline
2020-05-05 16:21:50.495 8813-8856/com.johnowais.wabah2020 I/Adreno: QUALCOMM build : 2df12b3, I07da2d9908
Build Date : 10/04/18
OpenGL ES Shader Compiler Version: EV031.25.03.01
Local Branch :
Remote Branch :
Remote Branch :
Reconstruct Branch :
2020-05-05 16:21:50.495 8813-8856/com.johnowais.wabah2020 I/Adreno: Build Config : S L 6.0.7 AArch64
2020-05-05 16:21:50.495 8813-8856/com.johnowais.wabah2020 D/vndksupport: Loading /vendor/lib64/hw/gralloc.msm8953.so from current namespace instead of sphal namespace.
2020-05-05 16:21:50.502 8813-8856/com.johnowais.wabah2020 I/Adreno: PFP: 0x005ff087, ME: 0x005ff063
2020-05-05 16:21:50.509 8813-8856/com.johnowais.wabah2020 I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
2020-05-05 16:21:50.509 8813-8856/com.johnowais.wabah2020 I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0
2020-05-05 16:21:50.511 8813-8856/com.johnowais.wabah2020 I/OpenGLRenderer: Initialized EGL, version 1.4
2020-05-05 16:21:50.511 8813-8856/com.johnowais.wabah2020 D/OpenGLRenderer: Swap behavior 2
2020-05-05 16:21:50.513 8813-8858/com.johnowais.wabah2020 D/NetworkSecurityConfig: No Network Security Config specified, using platform default
2020-05-05 16:21:50.515 8813-8858/com.johnowais.wabah2020 I/DpmTcmClient: RegisterTcmMonitor from: com.android.okhttp.TcmIdleTimerMonitor
2020-05-05 16:21:50.565 8813-8856/com.johnowais.wabah2020 D/vndksupport: Loading /vendor/lib64/hw/android.hardware.graphics.mapper@2.0-impl.so from current namespace instead of sphal namespace.
2020-05-05 16:21:50.566 8813-8856/com.johnowais.wabah2020 D/vndksupport: Loading /vendor/lib64/hw/gralloc.msm8953.so from current namespace instead of sphal namespace.
我无法获取数据,因为它的格式不正确。我也想使用 where 子句,它会给我我想要的州的每个地区的数据。
API link: https://api.covid19india.org/v2/state_district_wise.json
结构
JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, "https://api.covid19india.org/v2/state_district_wise.json",null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try {
for (int i = 0; i < response.length(); i++) {
JSONObject dataOBJ = response.getJSONObject(i);
JSONArray jsonChild = dataOBJ.getJSONArray("districtData");
for (int k = 0; k < jsonChild.length(); k++) {
JSONObject obj = jsonChild.getJSONObject(k);
int active = obj.getInt("active");
String district = obj.getString("district");
JSONObject child = obj.getJSONObject("delta");
int confirmed = child.getInt("confirmed");
}
}
} catch (Exception exp) {
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(request);
试试这个方法
StringRequest stringRequest = new StringRequest(Request.Method.GET,
"https://api.covid19india.org/v2/state_district_wise.json",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "onResponse: "+response);
try {
JSONArray responseArray = new JSONArray(response);
for (int i = 0; i < responseArray.length(); i++) {
JSONObject stateObject = responseArray.getJSONObject(i);
JSONArray districtArray = stateObject.getJSONArray("districtData");
for(int j=0; j<districtArray.length(); j++){
JSONObject districtObject = districtArray.getJSONObject(j);
//do what you want to do with district object
Log.d(TAG, "onResponse: "+districtObject.toString());
}
}
}catch (JSONException e){
Log.d(TAG, "onResponse: "+e.getMessage());
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);