2013-01-03 71 views
1

我使用下面的類來獲取一些JSON數據。奇怪的是,我無法僅從我需要測試應用程序的url(服務器端不是由我維護)解析JSON:如果您單擊this link,您將能夠在瀏覽器中看到JSON數據。但是當我嘗試以編程方式解析它時,它會引發JSONException。當試圖解析JSON數據時android JSONException

01-03 11:08:23.615: E/JSON Parser(19668): Error parsing data org.json.JSONException: Value <html><head><title>JBoss of type java.lang.String cannot be converted to JSONObject 

我試着用http://ip.jsontest.com/http://api.androidhive.info/contacts進行測試,它很好地工作!只有在嘗試從我的第一個鏈接解析JSON時才引發異常。我認爲這可能是一個服務器問題,但我能夠用瀏覽器查看JSON數據..只是無法解釋這一點。

public class JSONParser { 

static InputStream is = null; 
static JSONObject jObj = null; 
static String json = ""; 

/* default constuctor*/ 
public JSONParser() { 

} 

public JSONObject getJSONFromUrl(String url) { 

    /* get JSON via http request */ 
    try { 

     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost(url); 

     HttpResponse httpResponse = httpClient.execute(httpPost); 
     HttpEntity httpEntity = httpResponse.getEntity(); 
     is = httpEntity.getContent();   

    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    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(); 
     json = sb.toString(); 
    } catch (Exception e) { 
     Log.e("Buffer Error", "Error converting result " + e.toString()); 
    } 

    /* try to parse the string to JSON Object*/ 
    try { 
     jObj = new JSONObject(json); 
    } catch (JSONException e) { 
     Log.e("JSON Parser", "Error parsing data " + e.toString()); 
    } 

/* return JSON String */ 
    return jObj; 

     } 
    } 
+2

看起來您實際上正在收到JBoss錯誤消息。我想添加一個'System.err.println(json);'看看你實際上試圖解析什麼...... – beny23

+0

你的問題是在jObj = new JSONObject(json); ,檢查你的字符串'json'是否包含任何奇怪的符號或html。 –

+0

是...值 ..........但是爲什麼? – <span class="text-secondary"> <small> <a rel="noopener" target="_blank" href="https://stackoverflow.com/users/1199662/">Droidman</a></span> <span></span> </small> </span> </p> </div> </div> </div> </div> </div> </article> </div> <div class="answer-title"> <span class="text-logo margin-top-sm">A</span> <h2 class="title h4">回答</h2> </div> <div class="item-description text-md markdown-body margin-bottom-40 voidso"> <article class="board-top-1 padding-top-10"> <div class="post-col vote-info"> <span class="count">1<i class="fa fa-thumbs-up"></i></span> <i class="fa fa-check fa-2x"></i> </div> <div class="post-offset"> <div class="answer fmt"> <p>使用的BufferedReader辦法去給你拿的是網頁的HTML源代碼,這就是爲什麼你看到的HTML標籤,如< HTML> <頭> <標題>在您的字符串。相反,使用EntityUtils來獲取JSON字符串:</p> <pre><code class="prettyprint-override">DefaultHttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(url); try { HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity httpEntity = httpResponse.getEntity(); int status = httpResponse.getStatusLine().getStatusCode(); if (status == HttpStatus.SC_OK) { String jsonString = EntityUtils.toString(httpEntity); try { JSONObject jsonObject = new JSONObject(jsonString); } catch (JSONException e) { e.printStackTrace(); } } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } </code></pre> </div> <div class="post-info"> <div class="post-meta row"> <p class="text-secondary col-lg-6"> <span class="source"> <a rel="noopener" target="_blank" href="https://stackoverflow.com/q/14391474">來源</a> </span> </p> <p class="text-secondary col-lg-6"> <span class="float-right date"> <span>2013-01-18 02:19:27</span> </p> <p class="col-12"></p> <p class="col-12"></p></div> </div> <!-- comments --> <div class="comments"> <div itemprop="comment" class="post-comment"> <div class="row"> <div class="col-lg-1"><span class="text-secondary">+0</span></div> <div class="col-lg-11"> <p class="commenttext">當我嘗試.....在jsonString :: >>>我沒有得到整個數據.. 。你知道如何管理它嗎? – <span class="text-secondary"> <small> <span></span> </small> </span> </p> </div> </div> </div> <div itemprop="comment" class="post-comment"> <div class="row"> <div class="col-lg-1"><span class="text-secondary">+0</span></div> <div class="col-lg-11"> <p class="commenttext">, 「medium」:{ 「url」:「https://i.ytimg.com/vi/zUaMzFrRLWg/mqdefault.jpg」, 「width ...」最後,我得到了「...」如何獲取整個數據「 – <span class="text-secondary"> <small> <span></span> </small> </span> </p> </div> </div> </div> </div> </div> </article> <div> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-6208739752673518" data-ad-slot="1038284119" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> </div> <div class="clearfix"> </div> <div class="relative-box"> <div class="relative">相關問題</div> <ul class="relative_list"> <li> 1. <a href="http://hk.uwenku.com/question/p-nogsztmn-qg.html" target="_blank" title="解析json時,JSONException:值ï»"> 解析json時,JSONException:值ï» </a> </li> <li> 2. <a href="http://hk.uwenku.com/question/p-skamkene-zc.html" target="_blank" title="Android JSON解析「JSONException;值<!DOCTYPE"> Android JSON解析「JSONException;值<!DOCTYPE </a> </li> <li> 3. <a href="http://hk.uwenku.com/question/p-dvfhstml-ys.html" target="_blank" title="JSONException:試圖從JSON"> JSONException:試圖從JSON </a> </li> <li> 4. <a href="http://hk.uwenku.com/question/p-uzyelkca-tm.html" target="_blank" title="JSONException:沒有值(解析JSON圖像)"> JSONException:沒有值(解析JSON圖像) </a> </li> <li> 5. <a href="http://hk.uwenku.com/question/p-nkcmghrd-bcc.html" target="_blank" title="JSONException解析JSON字符串時:爲"> JSONException解析JSON字符串時:爲 </a> </li> <li> 6. <a href="http://hk.uwenku.com/question/p-bakwipbb-ok.html" target="_blank" title="SIGABRT當試圖解析JSON"> SIGABRT當試圖解析JSON </a> </li> <li> 7. <a href="http://hk.uwenku.com/question/p-zpvnwrjc-hd.html" target="_blank" title="解析JSON數據時出錯。 JSONException:在字符輸入結束"> 解析JSON數據時出錯。 JSONException:在字符輸入結束 </a> </li> <li> 8. <a href="http://hk.uwenku.com/question/p-bsuicetn-yo.html" target="_blank" title="Android JSON數據解析"> Android JSON數據解析 </a> </li> <li> 9. <a href="http://hk.uwenku.com/question/p-knrljqgw-zt.html" target="_blank" title="JSONexception同時使用JSON解析器的Android的Java"> JSONexception同時使用JSON解析器的Android的Java </a> </li> <li> 10. <a href="http://hk.uwenku.com/question/p-mjjhuujf-ou.html" target="_blank" title="解析json數據android"> 解析json數據android </a> </li> <div> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-6208739752673518" data-ad-slot="4606349252"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <li> 11. <a href="http://hk.uwenku.com/question/p-pegiydmk-qm.html" target="_blank" title="試圖解析iOS中的JSON數據"> 試圖解析iOS中的JSON數據 </a> </li> <li> 12. <a href="http://hk.uwenku.com/question/p-nndlmbto-s.html" target="_blank" title="試圖用python解析JSON數據"> 試圖用python解析JSON數據 </a> </li> <li> 13. <a href="http://hk.uwenku.com/question/p-sgxpnhxo-bcs.html" target="_blank" title="解析JSON數據給出JSONEXception:沒有內容值"> 解析JSON數據給出JSONEXception:沒有內容值 </a> </li> <li> 14. <a href="http://hk.uwenku.com/question/p-gfjfgrra-wh.html" target="_blank" title="JSONException解析一個有效的JSON"> JSONException解析一個有效的JSON </a> </li> <li> 15. <a href="http://hk.uwenku.com/question/p-kmekrshz-gu.html" target="_blank" title="解析json數據時出錯Android"> 解析json數據時出錯Android </a> </li> <li> 16. <a href="http://hk.uwenku.com/question/p-kanqadye-bnk.html" target="_blank" title="解析JSON數據"> 解析JSON數據 </a> </li> <li> 17. <a href="http://hk.uwenku.com/question/p-eaqgvrcl-bga.html" target="_blank" title="試圖解析Instagram JSON"> 試圖解析Instagram JSON </a> </li> <li> 18. <a href="http://hk.uwenku.com/question/p-npllckse-bdq.html" target="_blank" title="在Android中解析Google Places JSON數據?"> 在Android中解析Google Places JSON數據? </a> </li> <li> 19. <a href="http://hk.uwenku.com/question/p-comvtawd-bco.html" target="_blank" title="Android Json HTTP Post,無法解析數據"> Android Json HTTP Post,無法解析數據 </a> </li> <li> 20. <a href="http://hk.uwenku.com/question/p-wjxophhq-bdt.html" target="_blank" title="Android解析JSON並避免應用程序崩潰如果JSONException"> Android解析JSON並避免應用程序崩潰如果JSONException </a> </li> <li> 21. <a href="http://hk.uwenku.com/question/p-dyixhlgp-bkm.html" target="_blank" title="Android JSON不解析所有數據"> Android JSON不解析所有數據 </a> </li> <li> 22. <a href="http://hk.uwenku.com/question/p-mfuqhphb-sr.html" target="_blank" title="Java Android:如何將意圖數據解析爲JSOn格式?"> Java Android:如何將意圖數據解析爲JSOn格式? </a> </li> <li> 23. <a href="http://hk.uwenku.com/question/p-yerheybt-nc.html" target="_blank" title="json數據解析錯誤android studio"> json數據解析錯誤android studio </a> </li> <li> 24. <a href="http://hk.uwenku.com/question/p-wgrydbcf-bhd.html" target="_blank" title="列表視圖不顯示相應的數據,當解析json"> 列表視圖不顯示相應的數據,當解析json </a> </li> <li> 25. <a href="http://hk.uwenku.com/question/p-kcbhouwy-bhn.html" target="_blank" title="如何解析JSON數據到Android?"> 如何解析JSON數據到Android? </a> </li> <li> 26. <a href="http://hk.uwenku.com/question/p-phknzkqz-vh.html" target="_blank" title="Android ListView使用Json解析數據"> Android ListView使用Json解析數據 </a> </li> <li> 27. <a href="http://hk.uwenku.com/question/p-rklofngu-rv.html" target="_blank" title="Android Json錯誤解析數據"> Android Json錯誤解析數據 </a> </li> <li> 28. <a href="http://hk.uwenku.com/question/p-mxihktpi-bkx.html" target="_blank" title="解析JSON數組時解析Android:ArrayIndexOutOfBoundsException"> 解析JSON數組時解析Android:ArrayIndexOutOfBoundsException </a> </li> <li> 29. <a href="http://hk.uwenku.com/question/p-rkdeggqz-bak.html" target="_blank" title="解析JSON數據到列表視圖"> 解析JSON數據到列表視圖 </a> </li> <li> 30. <a href="http://hk.uwenku.com/question/p-mmjflojy-bhh.html" target="_blank" title="com.google.gson.JsonSyntaxException當試圖解析日期/時間JSON"> com.google.gson.JsonSyntaxException當試圖解析日期/時間JSON </a> </li> </ul> </div> <div> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-6208739752673518" data-ad-slot="1575177025"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <div class="padding-top-10"></div> </div> </div> <script type="text/javascript" src="http://img.uwenku.com/uwenku/script/side.js?t=1644592048261"></script> <script type="text/javascript" src="http://img.uwenku.com/uwenku/plugin/highlight/highlight.pack.js"></script> <link href="http://img.uwenku.com/uwenku/plugin/highlight/styles/docco.css" media="screen" rel="stylesheet" type="text/css" /> <script type="text/javascript"> $('pre').each(function(i, e) { hljs.highlightBlock(e, "<span class='indent'> </span>", false) }); </script> <div class="col-lg-3 col-md-4 col-sm-5"> <div id="rightTop"> <div class="row"> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-6208739752673518" data-ad-slot="5415218910" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <div class="row sidebar panel panel-default"> <div class="panel-heading font-bold"> 最新問題 </div> <div class="m-b-sm m-t-sm clearfix"> <ul class="side_article_list"> <li class="side_article_list_item"> 1. <a href="http://hk.uwenku.com/question/p-csmlyttv-tb.html" target="_blank" title="在角度工人中導入模塊?"> 在角度工人中導入模塊? </a> </li> <li class="side_article_list_item"> 2. <a href="http://hk.uwenku.com/question/p-qrtnwcvg-tn.html" target="_blank" title="Python布爾方法命名約定"> Python布爾方法命名約定 </a> </li> <li class="side_article_list_item"> 3. <a href="http://hk.uwenku.com/question/p-qvpcimgh-py.html" target="_blank" title="jquery background-image"> jquery background-image </a> </li> <li class="side_article_list_item"> 4. <a href="http://hk.uwenku.com/question/p-eyghaugq-qh.html" target="_blank" title="從s3檢索音頻時出錯"> 從s3檢索音頻時出錯 </a> </li> <li class="side_article_list_item"> 5. <a href="http://hk.uwenku.com/question/p-szhcyset-qt.html" target="_blank" title="如何將QR碼自動添加到圖像"> 如何將QR碼自動添加到圖像 </a> </li> <li class="side_article_list_item"> 6. <a href="http://hk.uwenku.com/question/p-oscplsex-rc.html" target="_blank" title="16個任務(1048.5 MB)的序列化結果的總大小大於spark.driver.maxResultSize(1024.0 MB)"> 16個任務(1048.5 MB)的序列化結果的總大小大於spark.driver.maxResultSize(1024.0 MB) </a> </li> <li class="side_article_list_item"> 7. <a href="http://hk.uwenku.com/question/p-sfvehlqe-rx.html" target="_blank" title="打字稿,使用類沒有構造"> 打字稿,使用類沒有構造 </a> </li> <li class="side_article_list_item"> 8. <a href="http://hk.uwenku.com/question/p-khjthsaj-pd.html" target="_blank" title="PHP通過除去空鍵"> PHP通過除去空鍵 </a> </li> <li class="side_article_list_item"> 9. <a href="http://hk.uwenku.com/question/p-nihddvmm-ne.html" target="_blank" title="Jest TypeError:無法讀取未定義的'商店'"> Jest TypeError:無法讀取未定義的'商店' </a> </li> <li class="side_article_list_item"> 10. <a href="http://hk.uwenku.com/question/p-fsfdvsjn-mm.html" target="_blank" title="在C++中有沒有一種慣用的方法來防止運行一組操作導致集合發生變化的情況?"> 在C++中有沒有一種慣用的方法來防止運行一組操作導致集合發生變化的情況? </a> </li> </ul> </div> </div> </div> <p class="article-nav-bar"></p> <div class="row sidebar article-nav"> <div class="row box_white visible-sm visible-md visible-lg margin-zero"> <div class="top"> <h3 class="title"><i class="glyphicon glyphicon-th-list"></i> 相關問題</h3> </div> <div class="article-relative-content"> <ul class="side_article_list"> <li class="side_article_list_item"> 1. <a href="http://hk.uwenku.com/question/p-nogsztmn-qg.html" target="_blank" title="解析json時,JSONException:值ï»"> 解析json時,JSONException:值ï» </a> </li> <li class="side_article_list_item"> 2. <a href="http://hk.uwenku.com/question/p-skamkene-zc.html" target="_blank" title="Android JSON解析「JSONException;值<!DOCTYPE"> Android JSON解析「JSONException;值<!DOCTYPE </a> </li> <li class="side_article_list_item"> 3. <a href="http://hk.uwenku.com/question/p-dvfhstml-ys.html" target="_blank" title="JSONException:試圖從JSON"> JSONException:試圖從JSON </a> </li> <li class="side_article_list_item"> 4. <a href="http://hk.uwenku.com/question/p-uzyelkca-tm.html" target="_blank" title="JSONException:沒有值(解析JSON圖像)"> JSONException:沒有值(解析JSON圖像) </a> </li> <li class="side_article_list_item"> 5. <a href="http://hk.uwenku.com/question/p-nkcmghrd-bcc.html" target="_blank" title="JSONException解析JSON字符串時:爲"> JSONException解析JSON字符串時:爲 </a> </li> <li class="side_article_list_item"> 6. <a href="http://hk.uwenku.com/question/p-bakwipbb-ok.html" target="_blank" title="SIGABRT當試圖解析JSON"> SIGABRT當試圖解析JSON </a> </li> <li class="side_article_list_item"> 7. <a href="http://hk.uwenku.com/question/p-zpvnwrjc-hd.html" target="_blank" title="解析JSON數據時出錯。 JSONException:在字符輸入結束"> 解析JSON數據時出錯。 JSONException:在字符輸入結束 </a> </li> <li class="side_article_list_item"> 8. <a href="http://hk.uwenku.com/question/p-bsuicetn-yo.html" target="_blank" title="Android JSON數據解析"> Android JSON數據解析 </a> </li> <li class="side_article_list_item"> 9. <a href="http://hk.uwenku.com/question/p-knrljqgw-zt.html" target="_blank" title="JSONexception同時使用JSON解析器的Android的Java"> JSONexception同時使用JSON解析器的Android的Java </a> </li> <li class="side_article_list_item"> 10. <a href="http://hk.uwenku.com/question/p-mjjhuujf-ou.html" target="_blank" title="解析json數據android"> 解析json數據android </a> </li> </ul> </div> </div> </div> </div> </div> </div> </div><!-- wrap end--> <!-- footer --> <footer id="footer"> <div class="bg-simple lt"> <div class="container"> <div class="row padder-v m-t"> <div class="col-xs-8"> <ul class="list-inline"> <li><a href="http://hk.uwenku.com/contact">聯系我們</a></li> <li>© 2020 HK.UWENKU.COM</li> <li><a target="_blank" href="https://beian.miit.gov.cn/">沪ICP备13005482号-4</a></li> <li><script type="text/javascript" src="https://v1.cnzz.com/z_stat.php?id=1280101193&web_id=1280101193"></script></li> <li><a href="http://www.uwenku.com/" target="_blank" title="优文库">简体中文</a></li> <li><a href="http://hk.uwenku.com/" target="_blank" title="優文庫">繁體中文</a></li> <li><a href="http://ru.uwenku.com/" target="_blank" title="поле вопросов и ответов">Русский</a></li> <li><a href="http://de.uwenku.com/" target="_blank" title="Frage - und - antwort - Park">Deutsch</a></li> <li><a href="http://es.uwenku.com/" target="_blank" title="Preguntas y respuestas">Español</a></li> <li><a href="http://hi.uwenku.com/" target="_blank" title="कार्यक्रम प्रश्न और उत्तर पार्क">हिन्दी</a></li> <li><a href="http://it.uwenku.com/" target="_blank" title="IL Programma di chiedere Park">Italiano</a></li> <li><a href="http://ja.uwenku.com/" target="_blank" title="プログラム問答園区">日本語</a></li> <li><a href="http://ko.uwenku.com/" target="_blank" title="프로그램 문답 단지">한국어</a></li> <li><a href="http://pl.uwenku.com/" target="_blank" title="program o park">Polski</a></li> <li><a href="http://tr.uwenku.com/" target="_blank" title="Program soru ve cevap parkı">Türkçe</a></li> <li><a href="http://vi.uwenku.com/" target="_blank" title="Đáp ứng viên">Tiếng Việt</a></li> <li><a href="http://fr.uwenku.com/" target="_blank" title="Programme interrogation Park">Française</a></li> </ul> </div> </div> </div> </div> </div> </footer> <!-- / footer --> <script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?f78a970f17b19a79fc477a3378096f29"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> </body> </html>