如何从 Volley Api 字符串响应中查找特定数据
How to find specific data from a Volley Api string response
我是 API 的新手,终于弄明白了如何从网站成功检索请求响应。问题是我完全不知道应该如何处理响应。我不知道如何访问响应中的某些值
这是我的API排球代码
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);
这是我收到的回复查询
"result":{10 items
"title":"Fortnite"
"releaseDate":"Jul 25, 2017"
"description":"Epic Games next project has you building forts and stopping a zombie invasion."
"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"
我如何从响应查询中找到显式值?我一直在网上寻找如何做这个,有很多不同的方法,我不知道该怎么做。例如,我如何才能将 发布日期 放入其自己的文本框中?当我使用字符串 response
时,我在网上看到的大多数示例都使用 JsonObjects
在您的 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();
}
}
我是 API 的新手,终于弄明白了如何从网站成功检索请求响应。问题是我完全不知道应该如何处理响应。我不知道如何访问响应中的某些值
这是我的API排球代码
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);
这是我收到的回复查询
"result":{10 items "title":"Fortnite" "releaseDate":"Jul 25, 2017" "description":"Epic Games next project has you building forts and stopping a zombie invasion." "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"
我如何从响应查询中找到显式值?我一直在网上寻找如何做这个,有很多不同的方法,我不知道该怎么做。例如,我如何才能将 发布日期 放入其自己的文本框中?当我使用字符串 response
时,我在网上看到的大多数示例都使用 JsonObjects在您的 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();
}
}