2016-11-28 79 views
0

,我用它来获得JSON从URL重定向到验证码,并提供了以下错误的URL:“数据org.json.JSONException:价值<!DOCTYPE of type java.lang.String cannot be converted to JSONObject错误的数据进行解析:org.json.JSONException:值<java.lang.String类型的DOCTYPE不能被转换为JSONObject的

以下是活动和代码中有我试图从获得JSON而是我重定向到验证码的网址:

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数据的页面?

+0

好像你正试图字符串转换成JSON对象,这就是为什么你得到这个error.Before转换的响应,JSON对象确保响应是JSON从。 – Vishwa

+0

再看这个问题我已经编辑它@Vishwa –

+0

你可以张贴此功能代码.. JSONfunctions .getJSONfromURL() – Vishwa

回答

0

尼尔森,你都可以从Web服务的HTML内容。响应格式不是由JSONException发生的有效json。

+0

再次看到问题我有 –

1

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

我试着打了URL提到它给下面的响应,

{"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或修复旧的。

+0

如果我对我的网络别人能也因此改变网络收到此错误是解决不了问题。像通常父母限制Facebook服务器,所以有孩子不能在家庭网络使用Facebook,所以在这种情况下,你不能责备Facebook的代码。尝试了解 –

+0

我最关心的是你的网络有一些防火墙,服务器无法通过编辑它 –

+0

@NelsonJohn Sahil Garg是正确的。当我尝试使用您的代码时,可以尝试使用其他网络连接。 – Vishwa

相关问题