android - JSONExecption 未终止的字符串 - Android

标签 android json

我正在构建一个连接到博客的应用程序,然后以 JSON 格式收集数据。目前我遇到了这个错误(很抱歉所有 JSON 数据都不确定是否包括在内):Exception Caught org.json.JSONException: Unterminated string at character 6564 of {"status":"ok","count":20,"count_total":1727,"pages":87,"posts":[{"id":23419,"url":"http:\/\/blog.teamtreehouse.com\/happy-mothers-day-ones-whove-shaped-web-careers","title":"Happy Mother\u2019s Day! Thanks, Mom, for Helping Us Learn","date":"2014-05-08 11:00:29","author":"Ryan Brinks","thumbnail":"http:\/\/blog.teamtreehouse.com\/wp-content\/uploads\/2014\/05\/mothers-dayHaik-Avanian-150x150.jpg"},{"id":23412,"url":"http:\/\/blog.teamtreehouse.com\/technology-brings-people-attitude-public-data-projects","title":"Public Data Brings ‘We the People’ Attitude to Technology","date":"2014-05-08 10:08:22","author":"Kelley King","thumbnail":"http:\/\/blog.teamtreehouse.com\/wp-content\/uploads\/2014\/05\/adoptahydrant-150x150.jpg"},{"id":23409,"url":"http:\/\/blog.teamtreehouse.com\/help-students-learn-computer-programming","title":"A Push for More Computer Programming in Public Schools","date":"2014-05-07 15:50:51","author":"Tim Skillern","thumbnail":"http:\/\/blog.teamtreehouse.com\/wp-content\/uploads\/2014\/05\/student-computer-class-woodleywonderworks-flickr-150x150.jpg"},{"id":23398,"url":"http:\/\/blog.teamtreehouse.com\/military-veterans-finding-technology-jobs-secure-bet","title":"Technology Jobs a Secure Bet for Military Veterans","date":"2014-05-06 13:45:13","author":"Anayat Durrani","thumbnail":"http:\/\/blog.teamtreehouse.com\/wp-content\/uploads\/2014\/05\/durrani-kopser-150x150.jpg"},{"id":23407,"url":"http:\/\/blog.teamtreehouse.com\/typography-sidebars-style-guides-treehouse-show-ep-89","title":"Typography, Sidebars, Style Guides | The Treehouse Show Ep 89","date":"2014-05-06 10:15:43","author":"Jason Seifer","thumbnail":null},{"id":23393,"url":"http:\/\/blog.teamtreehouse.com\/5-tips-creating-perfect-web-design-portfolio","title":"5 Tips for Creating the Perfect Web Design Portfolio","date":"2014-05-05 17:55:08","author":"Nick Pettit","thumbnail":"http:\/\/blog.teamtreehouse.com\/wp-content\/uploads\/2014\/05\/how-to-make-a-website-150x150.jpg"},{"id":23381,"url":"http:\/\/blog.teamtreehouse.com\/writing-tips-better-business-marketing","title":"11 Rules for Better Writing, or How Not to Use a Thesaurus","date":"2014-05-01 18:38:32","author":"Tim Skillern","thumbnail":"http:\/\/blog.teamtreehouse.com\/wp-content\/uploads\/2014\/05\/pencils-wikimedia-150x150.jpg"},{"id":23387,"url":"http:\/\/blog.teamtreehouse.com\/web-job-perks-unlimited-vacation-catered-lunch-part-amazing-opportunity-weebly-com-programmer","title":"Web Job Perks: Unlimited Vacation, Catered Lunch Part of \u2018Amazing Opportunity\u2019 for Weebly.com Programmer","date":"2014-05-01 17:00:28","author":"Jimmy Alford","thumbnail":"http:\/\/blog.teamtreehouse.com\/wp-content\/uploads\/2014\/05\/weebly-guy0-2-150x150.jpg"},{"id":23375,"url":"http:\/\/blog.teamtreehouse.com\/illustrator-ben-obrien-inspiration","title":"Noted Illustrator Ben O’Brien Talks About Finding Inspiration, Taking Chances","date":"2014-04-29 18:13:58","author":"Gillian Carson","thumbnail":"http:\/\/blog.teamtreehouse.com\/wp-content\/uploads\/2014\/04\/obrien3-150x150.jpg"},{"id":23373,"url":"http:\/\/blog.teamtreehouse.com\/gulp-sketch-3-bud-treehouse-show-episode-88","title":"Gulp | Sketch 3 | Bud | The Treehouse Show Episode 88","date":"2014-04-29 15:29:20","author":"Jason Seifer","thumbnail":null},{"id":23361,"url":"http:\/\/blog.teamtreehouse.com\/flexbox-next-generation-css-layout-arrived","title":"Flexbox: The Next Generation of CSS Layout Has Arrived","date":"2014-04-29 11:53:40","author":"Nick Pettit","thumbnail":"http:\/\/blog.teamtreehouse.com\/wp-content\/uploads\/2014\/04\/Screen-Shot-2014-04-28-at-1.00.03-AM-150x150.png"},{"id":23364,"url":"http:\/\/blog.teamtreehouse.com\/help-wanted-women-color-needed-technology-web-jobs","title":"Help Wanted: Women of Color Needed in Technology, Web Jobs","date":"2014-04-28 12:28:56","author":"Anayat Durrani","thumbnail":"http:\/\/blog.teamtreehouse.com\/wp-content\/uploads\/2014\/04\/poorn
这是捕获错误的地方:

public void updateList() {
        if (mBlogData == null) {
            // TODO: Handle Error
        }
        else {
            try {
            Log.d(TAG, mBlogData.toString(2));
            }
            catch (JSONException e) {
                Log.e(TAG, "Exception Caught", e);
            }
        }
    }

我不确定是什么导致了这个错误,所以欢迎提出任何建议。如果需要,我可以提供更多代码。谢谢。

最佳答案

只是想添加到 eMad 的答案中,它帮助我解决了你遇到的同样的问题。我希望这对任何追随我的人有所帮助,因为这个该死的 bug 杀死了我一天中的 2 个小时(或晚上,我是夜间 Activity 的)。好吧,没有进一步(ado?再见?),给你:P.S.下面的代码将进入您的私有(private)类(class) GetBlogPostsTask AsynnTask...

protected JSONObject doInBackground(Object... arg0) {
        int responseCode = -1;
        JSONObject jsonResponse = null;

        try {
            //set API URL
            URL blogFeedUrl = new URL("http://blog.teamtreehouse.com/api/get_recent_summary/?    count=" + NUMBER_OF_POSTS);

            //open URL connection
            URLConnection connection = blogFeedUrl.openConnection();
            //create BufferedReader to read the InputStream return from the connection
            BufferedReader in = new BufferedReader(
                    new InputStreamReader ( connection.getInputStream() )
            );

            //initiate strings to hold response data
            String inputLine;
            String responseData = "";
            //read the InputStream with the BufferedReader line by line and add each line to responseData
            while ( ( inputLine = in.readLine() ) != null ){
                responseData += inputLine;
            }

            //check to make sure the responseData is not empty
            if( responseData!= "" ){
                /*initiate the jsonResponse as a JSONObject based on the string values added
                 to responseData by the BufferedReader */
                jsonResponse = new JSONObject(responseData);
            }

            /*return the jsonResponse JSONObject to the postExecute() method
            to update the UI of the context */
            return jsonResponse;
        }
        catch (MalformedURLException e) {
            Log.e(TAG, "Exception caught: ", e);
        }
        catch (IOException e) {
            Log.e(TAG, "Exception caught: ", e);
        }
        catch (Exception e) {
            Log.e(TAG, "Exception caught: ", e);
        }

        return jsonResponse;
    }

    @Override
    protected void onPostExecute(JSONObject result) {
        /* set the class' member JSONObject mBlogData to the result
         to be used by the handleBlogResponse() method to update the UI */
        mBlogData = result;
        /*call the handleBlogResponse() method to update the UI with the result of this AsyncTask
          which will be a JSONObject in best case scenario or a null object in worst case */
        handleBlogResponse();
    }

关于android - JSONExecption 未终止的字符串 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23586370/

相关文章:

java.lang.RuntimeException : java. lang.RuntimeException : Duplicate class bolts. 在模块 jetified-bolts-tasks-1.24.1.jar 中发现 AggregateException

node.js - 如何禁用 `npm install --prefix` 的本地包依赖?

javascript - Javascript 的 JSON 解析问题

android - Flash Builder 中的哪个元素定义 iPhone 应用程序快捷方式名称?

Android:每 1 分钟更新一次 ListView 项目

json - Flutter:JSON 循环

javascript - 使用 PHP 和 Json 的动态 dhtmlx 组织结构图

android - 如何从 GridView 中的图标启动应用程序?

android - Facebook Session 对象具有 "email"权限,但 Request.executeMeRequestAsync(...) 不返回电子邮件

android - picasso 无法在@override 上加载动画