我是 API 新手,终于弄清楚如何成功从网站检索请求响应。问题是我完全不知道该如何处理这个回应。我不知道如何访问响应中的某些值
这是我的 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);
这是我收到的响应查询
"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"
如何从响应查询中查找显式值?我一直在网上寻找如何做到这一点,有很多不同的方法可以进行,但我不知道该怎么做。例如,我如何才能将发布日期放入其自己的文本框中?当我使用字符串响应时,我在网上看到的大多数示例都使用 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();
}
}
关于java - 如何从 Volley Api 字符串响应中查找特定数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59252855/