android - 如何将一个 API 重放的 JSON 中的值放到另一个 API post 方法的主体中?

标签 android json http post android-volley

我有这个项目,我在其中获得了一些 url/端点/API 作为 Assets 。我想获取在第一个 API 的 JSON 数组(“结果”)的每个对象中找到的字符串的值(“id”),并将其用作另一个 API 调用的正文参数。

我有一个 API(获取所有点),在该 API 中,我在正文中传递一个标记,然后我得到一个带有结果数组的 JSON 对象(这些结果是一些点)。一切都很好。问题在于:对于我的下一个 API(获取现场详细信息),我需要遍历前一个 API 响应的“结果”数组中的每个对象(现场)并获取其“id”值。然后我将使用该 id 作为 Get spot details API 的 body 参数,这样当我从 spot 列表中单击一个项目时,它将打开一个包含该 spot 详细信息的新 Activity 。

我尝试将 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;
}

最佳答案

为什么不使用强类型类并使用 json 库进行序列化和反序列化,而不是解释请求/响应。

您还可以将共享汇编器与契约(Contract)类一起使用,以防您修改这两个项目或与作者合作。

关于android - 如何将一个 API 重放的 JSON 中的值放到另一个 API post 方法的主体中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55819933/

相关文章:

android - 在 Nexus 7 800*1280 上使用 sw720 代替 hdpi 值

java - https请求,Android中的认证

android - Google 登录关闭时从上到下出现黑色条纹 - Flutter

.net - 为什么不识别 HttpUtility 类?

forms - 应用程序/x-www-form-urlencoded 和字符集 ="utf-8"?

android - 如何从雅虎的天气API解析日期到Java Date

java - 如何通过更改其中的一个特定字段来创建新的 json 字符串?

java - org.w3c.dom.Node 的 json 实现

ruby-on-rails - 如何在compoundjs Controller 中从json检索数据

html - 使用 Delphi 2009 下载任何网页并将其保存为 Unicode?