【发布时间】:2019-02-19 12:10:11
【问题描述】:
我正在使用 Volley 请求 unsplash API,但是当我尝试以 JsonObjectRequest 请求它时,它没有给我任何错误,但我知道这是错误的方法,因为我收到的数据是 JSONArray
我的方法
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(
Request.Method.GET,
URL,
null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
if(response!=null){
// Process the JSON
try{
// Loop through the array elements
for (int i = 0; i < response.length(); i++) {
JSONObject js = response.getJSONObject(i);
Log.d("TESTING",js.getString("id"));
}
p.dismiss();
}catch (JSONException e){
e.printStackTrace();
}
}}
},
new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error){
Log.d("TESTING",error.getMessage());
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsonArrayRequest);
日志 由于 JSONArray 太大,无法在此处发布,这是格式
D/TESTING: org.json.JSONException: Value {"total": 44760,
"total_pages": 4476,
"results":[{JSONObjects}]}
如果您想查看 JSONArray,请点击链接“https://api.unsplash.com/search/photos?query=wood&client_id=ACESS_KEY_REQUIRED”
我也看过this的问题,但这完全不同
您只需创建一个帐户here即可申请密钥。
【问题讨论】: