【问题标题】:I can't parse json from Instagram api我无法从 Instagram api 解析 json
【发布时间】: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。
  • 不,它没有认证,可能是你说的

标签: android json parsing


【解决方案1】:

【讨论】:

  • 虽然您的答案是 100% 正确的,但如果该链接被移动、更改、合并到另一个链接或主站点消失,它也可能 100% 无用......因此,请edit您的答案,并将链接中的相关步骤复制到您的答案中,从而保证您的答案在本网站的生命周期内 100%! ;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多