【发布时间】:2019-09-13 03:41:06
【问题描述】:
我有这个项目,我在其中获得了一些 url/endpoints/API 作为资产。我想获取在第一个 API 的 JSON 数组(“结果”)的每个对象中找到的字符串的值(“id”),并将其用作另一个 API 调用的主体参数。
我有一个 API(获取所有点),其中我在正文中传递一个标记,并获得一个带有结果数组的 JSON 对象(这些结果是一些点)。一切都很好。这是问题所在:对于我的下一个 API(获取点详细信息),我需要从上一个 API 响应中遍历“结果”数组中的每个对象(点)并获取其“id”值。然后,我将使用该 id 作为 Get spot details API 的主体参数,因此当我从点列表中单击一个项目时,它将打开一个包含该点详细信息的新活动。
我尝试将 id 放在 ArrayList 中,使其成为静态等,但是当我尝试将 id 作为参数传递时,它给出了错误代码 500。
//Get all spots result with the array in which I have to get each "id" value
{
"result": [
{
"id": "0dEGTxlKxh",
"name": "27-Waam",
"country": "India",
"whenToGo": "APRIL",
"isFavorite": true
},
{
"id": "s7uMTLEM6g",
"name": "2nd Bay",
"country": "Morocco",
"whenToGo": "AUGUST",
"isFavorite": true
}, ...
}
//The id (taken from the prvious list of spots) that I need to put in the body of Get spot details POST request
{
"spotId": "DqeOFHedSe"
}
//Example of result by Get spot details API with the id of the clicked spot in the list
{
"result": {
"id": "DqeOFHedSe",
"name": "Pocitas Beach",
"longitude": -81.08,
"latitude": -4.11,
"windProbability": 87,
"country": "Ecuador",
"whenToGo": "OCTOBER",
"isFavorite": true
}
}
//Request details using volley for the clicked spot
try {
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
StringRequest requestDetails = new StringRequest(Request.Method.POST, GET_SPOT_DETAILS,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject result = new JSONObject(response);
SpotInfo.name = result.getString("name");
SpotInfo.country = result.getString("country");
/*SpotInfo.latitude = result.getDouble("latitude");
SpotInfo.longitude = result.getDouble("longitude");
SpotInfo.windPbb = result.getDouble("windProbability");*/
SpotInfo.whenToGo = result.getString("whenToGo");
} catch (JSONException e) {
Log.d(TAG, "Error when parsing all details in JSON.", e);
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "OnErrorResponse", error);
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("spotId", idArray.get(position)); //I think here is the problem
return params;
}
};
queue.add(requestDetails);
startActivity(new Intent(MainActivity.this, SpotInfo.class));
}
});
} catch(
Exception e) {
Log.e("OnItemClick", "OnItemClick error.", e);
}
//Iterating through the spots provided by Get allspots request
private static ArrayList<KitesurfingSpot> getAllSpotsFromJsonResult(String spotJSON) {
if(TextUtils.isEmpty(spotJSON))
return null;
ArrayList<KitesurfingSpot> spots = new ArrayList<>();
try {
JSONObject baseJsonResponse = new JSONObject(spotJSON);
JSONArray resultsArray = baseJsonResponse.getJSONArray("result");
for(int i=0; i < resultsArray.length(); i++) {
JSONObject spot = resultsArray.getJSONObject(i);
idStrings.add(spot.getString("id")); //Here I get the id
String name = spot.getString("name");
String country = spot.getString("country");
KitesurfingSpot kitesurfingSpot = new KitesurfingSpot(name, country, R.drawable.star_off);
spots.add(kitesurfingSpot);
}
} catch (JSONException e) {
Log.e(TAG, "Problem parsing the kitesurfing spot JSON result.", e);
}
return spots;
}
【问题讨论】:
标签: android json http post android-volley