【问题标题】:Error Parsing Data: org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject解析数据时出错:org.json.JSONException:Java.lang.String 类型的值 <!DOCTYPE 无法转换为 JSONObject
【发布时间】:2017-04-12 00:25:57
【问题描述】:

我用来从中获取 JSON 的 URL 将 URL 重定向到 recaptcha 并给出以下错误:“data org.json.JSONException: Value &lt;!DOCTYPE of type java.lang.String cannot be converted to JSONObject

以下是活动,代码中有我试图从中获取 JSON 的 url,但我被重定向到 recaptcha:

protected Void doInBackground(Void... params) {
            // Create an array
            arraylist = new ArrayList<HashMap<String, String>>();
            // Retrieve JSON Objects from the given URL address
//URL

                jsonobject = JSONfunctions
                        .getJSONfromURL("http://laboneinside.com/android/diag_conn.php");

            try {
                // Locate the array name in JSON
                jsonarray = jsonobject.getJSONArray("result");
                Log.i("JSON TAG", jsonobject.toString());

                for (int i = 0; i < jsonarray.length(); i++) {
                    HashMap<String, String> map = new HashMap<String, String>();
                    jsonobject = jsonarray.getJSONObject(i);
                    // Retrive JSON Objects
                    map.put("nameDiag", jsonobject.getString("name"));
                    map.put("srcDiag", jsonobject.getString("src"));
                    map.put("linkDiag", jsonobject.getString("link"));
                    // Set the JSON Objects into the array
                    arraylist.add(map);
                }
            } catch (JSONException e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }

JSONFUNCTION:

public class JSONfunctions {

    public static JSONObject getJSONfromURL(String url) {
        InputStream is = null;
        String result = "";
        JSONObject jArray = null;

        // Download JSON data from URL
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url);
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

        } catch (Exception e) {
            Log.e("log_tag", "Error in http connection " + e.toString());
        }

        // Convert response to string
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();
            Log.i("JSON Parser", result);
        } catch (Exception e) {
            Log.e("log_tag", "Error converting result " + e.toString());
        }

        try {

            jArray = new JSONObject(result);

        } catch (JSONException e) {
            Log.e("log_tag", "Error parsing data " + e.toString());
        }

        return jArray;
    }
}

以下是logcat:

I/JSON Parser: <!DOCTYPE html>
                                                                          <!--[if lt IE 7]> <html class="no-js ie6 oldie" lang="en-US"> <![endif]-->
                                                                          <!--[if IE 7]>    <html class="no-js ie7 oldie" lang="en-US"> <![endif]-->
                                                                          <!--[if IE 8]>    <html class="no-js ie8 oldie" lang="en-US"> <![endif]-->
                                                                          <!--[if gt IE 8]><!--> <html class="no-js" lang="en-US"> <!--<![endif]-->
                                                                          <head>
                                                                          <title>Attention Required! | CloudFlare</title>
                                                                          <meta charset="UTF-8" />
                                                                          <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
                                                                          <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
                                                                          <meta name="robots" content="noindex, nofollow" />
                                                                          <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1" />
                                                                          <link rel="stylesheet" id="cf_styles-css" href="/cdn-cgi/styles/cf.errors.css" type="text/css" media="screen,projection" />
                                                                          <!--[if lt IE 9]><link rel="stylesheet" id='cf_styles-ie-css' href="/cdn-cgi/styles/cf.errors.ie.css" type="text/css" media="screen,projection" /><![endif]-->
                                                                          <style type="text/css">body{margin:0;padding:0}</style>
                                                                          <!--[if lte IE 9]><script type="text/javascript" src="/cdn-cgi/scripts/jquery.min.js"></script><![endif]-->
                                                                          <!--[if gte IE 10]><!--><script type="text/javascript" src="/cdn-cgi/scripts/zepto.min.js"></script><!--<![endif]-->
                                                                          <script type="text/javascript" src="/cdn-cgi/scripts/cf.common.js"></script>


                                                                          </head>
                                                                          <body>
                                                                            <div id="cf-wrapper">
                                                                              <div class="cf-alert cf-alert-error cf-cookie-error" id="cookie-alert" data-translate="enable_cookies">Please enable cookies.</div>
                                                                              <div id="cf-error-details" class="cf-error-details-wrapper">
                                                                                <div class="cf-wrapper cf-header cf-error-overview">
                                                                                  <h1 data-translate="challenge_headline">One more step</h1>
                                                                                  <h2 class="cf-subheadline"><span data-translate="complete_sec_check">Please complete the security check to access</span> laboneinside.com</h2>
                                                                                </div><!-- /.header -->

                                                                                <div class="cf-section cf-highlight cf-captcha-container">
                                                                                  <div class="cf-wrapper">
                                                                                    <div class="cf-columns two">
                                                                                      <div class="cf-column">
                                                                                        <div class="cf-highlight-inverse cf-form-stacked">
                                                                                          <form class="challenge-form" id="challenge-form" action="/cdn-cgi/l/chk_captcha" method="get">
                                                                            <script type="text/javascript" src="/cdn-cgi/scripts/cf.challenge.js" data-type="normal"  data-ray="308cf17ac17329be" async data-sitekey="6LfOYgoTAAAAAInWDVTLSc8Yibqp-c9DaLimzNGM" data-stoken="kvvwvLSUth2MQ7fsfXFwr9TjwkJ6pEiMAfiAhb7x9fVk7yqCvGMh-myXoKQuhQt_4MjgZHunito0fikCnHS-DFoymBKWm5gb3lnlB4dceb0"></script>
                                                                            <div class="g-recaptcha"></div>
                                                                            <noscript id="cf-captcha-bookmark" class="cf-captcha-info">
                                                                              <div><div style="width: 302px">
                                                                                <div>
                                                                                  <iframe src="https://www.google.com/recaptcha/api/fallback?k=6LfOYgoTAAAAAInWDVTLSc8Yibqp-c9DaLimzNGM&stoken=kvvwvLSUth2MQ7fsfXFwr9TjwkJ6pEiMAfiAhb7x9fVk7yqCvGMh-myXoKQuhQt_4MjgZHunito0fikCnHS-DFoymBKWm5gb3lnlB4dceb0" frameborder="0" scrolling="no" style="width: 302px; height:422px; border-style: none;"></iframe>
                                                                                </div>
                                                                                <div style="width: 300px; border-style: none; bottom: 12px; left: 25px; margin: 0px; padding: 0px; right: 25px; background: #f9f9f9; border: 1px solid #c1c1c1; border-radius: 3px;">
                                                                                  <textarea id="g-recaptcha-response" name="g-recaptcha-response" class="g-recaptcha-response" style="width: 250px; height: 40px; border: 1px solid #c1c1c1; margin: 10px 25px; padding: 0px; resize: none;"></textarea>
                                                                                  <input type="submit" value="Submit"></input>
                                                                                </div>
                                                                              </div></div>
                                                                            </noscript>
                                                                          </form>

                                                                                        </div>
                                                                                      </div>

                                                                                      <div class="cf-column">
                                                                                        <div class="cf-screenshot-container">

                                                                                          <span class="cf-no-screenshot"></span>

                                                                                        </div>
                                                                                      </div>
                                                                                    </div><!-- /.columns -->
                                                                                  </div>
                                                                                </div><!-- /.captcha-container -->

                                                                                <div class="cf-section cf-wrapper">
                                                                                  <div class="cf-columns two">
                                                                                    <div class="cf-column">
                                                                                      <h2 data-translate="why_captcha_headline">Why do I have to complete a CAP
11-28 04:55:15.976 30265-30325/com.example.predator.labone E/log_tag: Error parsing data org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject

如何阻止它重定向,以便我可以访问包含 JSON 数据的页面?

【问题讨论】:

  • 您似乎正在尝试将字符串转换为 JSON 对象,这就是您收到此错误的原因。在将响应转换为 JSON 对象之前,请确保响应来自 JSON。
  • 再次查看问题我已经编辑了它@Vishwa
  • 你能把这个功能代码贴出来吗.. JSONfunctions .getJSONfromURL()
  • 看我的回答,希望对你有帮助
  • @Vishwa 我已经发布了函数代码

标签: php android json recaptcha cloudflare


【解决方案1】:

验证码的想法是防止屏幕抓取和机器人攻击。根据您所面临的情况,Cloudflare 了解您的呼叫是机器人,并自动将其重定向到验证码页面。要求 API 提供商使用页面规则删除对 API 的机器人保护。更多详情请阅读这篇文章https://support.cloudflare.com/hc/en-us/articles/200504045-Using-CloudFlare-with-your-API

【讨论】:

    【解决方案2】:

    您正在使用的网络应用了某种类型的防火墙, 请仔细阅读 HTML,“Please complete the security check to access

    我尝试点击提到的网址并给出以下响应,

    {"result":[{"id":"1","name":"Acer V3-571 Schematic Diagram","src":"https:\/\/www.laboneinside.com\/wp-content\/uploads\/2016\/11\/Acer-V3-571-Schematic-Diagram-620x330.png","link":"https:\/\/www.laboneinside.com\/acer-v3-571-schematic-diagram\/"}]}
    

    尝试使用其他网络/WIFI 或修复旧的。

    【讨论】:

    • 如果我在我的网络上遇到此错误,其他人也可以,因此更改网络不是解决方案
    • 我主要担心的是您的网络有一些您的服务器无法通过的防火墙。就像通常父母限制 facebook 服务器一样,孩子不能在家庭网络中使用 facebook,所以在这种情况下,你不能责怪 facebook 那里的代码。试着理解
    • @NelsonJohn Sahil Garg 是对的。当我尝试使用您的代码时,它正在工作。尝试使用其他网络连接。
    【解决方案3】:

    Nelson 您正在从 Web 服务获取 html 内容。响应格式不是一个有效的 json 由 JSONException 发生。

    【讨论】:

      猜你喜欢
      • 2016-08-28
      • 2013-02-10
      • 2015-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-20
      • 1970-01-01
      相关资源
      最近更新 更多