我有两个图像按钮,我的JSON响应是一个对象数组,我想通过单击逐个访问这两个对象。通过这样做我只获得第二个位置,但我想点击第一个按钮我想要第一个对象,而第二个按钮我想要第二个对象。
在解析数据时,我只获得第二个位置,仅限光类型对象。
final String potentialId = SitesUtil.getInstance().getSelectedPotentialId();
String urlOne = UrlUtility.getActionUrl(potentialId, cameraId);
RequestQueue requestQueue = Volley.newRequestQueue(getContext());
final StringRequest stringRequest = new StringRequest(Request.Method.GET, urlOne, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Gson gson = new Gson();
ActionButton[] actionButton = gson.fromJson(response, ActionButton[].class);
for (ActionButton actionButton1 : actionButton) {
getOnUrl = actionButton1.getOnUrl();
getOffUrl = actionButton1.getOffUrl();
getType = actionButton1.getType();
message = actionButton1.getOnSuccessMessage();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(stringRequest);
获取响应代码:
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.imageButton:
if (getType.equals("Strobe")) {
RequestQueue requestQueue = Volley.newRequestQueue(getContext());
StringRequest stringRequest = new StringRequest(Request.Method.GET, getOnUrl, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(getContext(), message, Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(stringRequest);
}
break;
case R.id.imageButton2:
if (getType.equals("Light")) {
RequestQueue requestQueue = Volley.newRequestQueue(getContext());
StringRequest stringRequest = new StringRequest(Request.Method.GET, getOnUrl, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(getContext(), message, Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(stringRequest);
}
break;
}
}
我的JSON响应:
[
{
"id": 28248,
"cameraId": 19316,
"onUrl": "",
"offUrl": "",
"statusUrl": "",
"type": "Strobe",
"statusOnMessage": "P6 = on",
"statusOffMessage": "P6 = off",
"onSuccessMessage": "P6 = on",
"offSuccessMessage": "P6 = off"
},
{
"id": 28844,
"cameraId": 19316,
"onUrl": "",
"offUrl": "",
"statusUrl": "",
"type": "Light",
"statusOnMessage": "P2 = on",
"statusOffMessage": "P2 = off",
"onSuccessMessage": "P2 = on",
"offSuccessMessage": "P2 = off"
}
]