简体   繁体   English

如何从 Volley Api 字符串响应中查找特定数据

[英]How to find specific data from a Volley Api string response

I am new to APIs and finally figured out how to successfully retrieve a request response from a website.我是 API 的新手,终于想出了如何从网站成功检索请求响应。 The thing is I am completely lost on how I should handle the response.问题是我完全不知道我应该如何处理响应。 I don't know how to access certain values within the response我不知道如何访问响应中的某些值

here is my API Volley code这是我的 API Volley 代码

  RequestQueue requestQueue = Volley.newRequestQueue(this);
    String uri = Uri.parse("https://chicken-coop.p.rapidapi.com/games/Fortnite?platform=pc")
            .buildUpon()
            .build().toString();

    StringRequest stringRequest = new StringRequest(
            Request.Method.GET, uri, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            displayResults.setText("Response: " + response.substring(0,500));

        }

    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
             displayResults.setText( "" + error.toString());
        }

    }) {

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            params.put("X-RapidAPI-Key", "5cdb2bbe57mshd9242c8d3177cb3p16f2fbjsnd7c5829eb4ad");
            params.put("X-RapidAPI-Host", "chicken-coop.p.rapidapi.com");
            return params;
        }
    };
    requestQueue.add(stringRequest);

Here is the response query that I received这是我收到的响应查询

"result":{10 items "title":"Fortnite" "releaseDate":"Jul 25, 2017" "description":"Epic Games next project has you building forts and stopping a zombie invasion." "result":{10 items "title":"Fortnite" "releaseDate":"Jul 25, 2017" "description":"Epic Games 的下一个项目让你建造堡垒并阻止僵尸入侵。" "genre":[... ]6 items "image":" https://static.metacritic.com/images/products/games/5/c7eb46ceb7da9c72c5a95193e8621faf-98.jpg " "score":81 "developer":"Epic Games" "publisher":[... ]1 item "rating":"T" "alsoAvailableOn":[6 items 0: "iPhone/iPad" 1: "PlayStation 3" 2: "PlayStation 4" 3: "Switch" 4: "Xbox 360" 5: "Xbox One" “流派”:[... ]6 项“图像”:“ https://static.metacritic.com/images/products/games/5/c7eb46ceb7da9c72c5a95193e8621faf-98.jpg ”“分数”:81“开发者”:” Epic Games" "publisher":[...]1 项 "rating":"T" "alsoAvailableOn":[6 项 0: "iPhone/iPad" 1: "PlayStation 3" 2: "PlayStation 4" 3: "开关”4:“Xbox 360” 5:“Xbox One”

How would I go about finding an explicit value from the response query ?我将如何从响应查询中找到显式值 I have been searching for how to do this online and there are so many different ways to go about and I have no clue what to do.我一直在网上寻找如何做到这一点,有很多不同的方法可以去做,但我不知道该怎么做。 For example, how would I be able to put the Release Date into its own text box?例如,我如何才能将发布日期放入其自己的文本框中? Most of the examples I see online use JsonObjects when I m using a string response当我使用字符串响应时,我在网上看到的大多数示例都使用JsonObjects

in your onResponse method you need to parse your result so you can extract any data you want在您的onResponse方法中,您需要解析您的结果,以便您可以提取您想要的任何数据

public void onResponse(String response) {
        try {
            JSONObject jsonObject = new JSONObject(response);
            JSONArray jsonArray = jsonObject.getJSONArray("result");
             // toaccess to your json data 
                String title = jsonArray.getString("title");
                // your code 


        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM