【发布时间】:2021-10-28 14:48:15
【问题描述】:
我无法从 instagram api 解析 json 并且我的代码出现此错误:
com.android.volley.ParseError: org.json.JSONException:
Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
Instagram API 链接:
https://www.instagram.com/pr.rezaabiri/?__a=1
这是我的 JSON 解析代码:
JsonObjectRequest jsonObjectRequest = new
JsonObjectRequest(Request.Method.GET, url, null, response - > {
try {
JSONObject graphql = response.getJSONObject("graphql");
JSONObject user = graphql.getJSONObject("user");
JSONObject edge_followed_by = user.getJSONObject("edge_followed_by");
JSONObject edge_follow = user.getJSONObject("edge_follow");
JSONObject edge_owner_to_timeline_media = user.getJSONObject("edge_owner_to_timeline_media");
posts.setText(edge_owner_to_timeline_media.getString("count"));
followers.setText(edge_followed_by.getString("count"));
following.setText(edge_follow.getString("count"));
txtAc.setText(user.getString("username"));
Picasso.get().load(user.getString("profile_pic_url_hd")).into(imageProfile);
} catch (JSONException e) {
e.printStackTrace();
}
}, error - > Log.e("err:", error.toString()));
jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(5000, 1, 1));
Volley.newRequestQueue(getApplicationContext()).add(jsonObjectRequest);
【问题讨论】:
-
您正在从该 API 请求接收 HTML,而不是 JSON。确保在尝试解析之前从该 API 请求中获取 JSON。
-
这段代码两天前有效
-
API 请求是否有身份验证?您是否发送了有效的 API 密钥或登录凭据?很可能您收到的是登录页面,而不是您期望的 JSON。
-
不,它没有认证,可能是你说的