JSONExecption未终止字符串 - Android

时间:2014-05-10 21:12:42

标签: android json

我正在构建一个连接到博客的应用程序,然后收集JSON中的数据。目前我正在编写此错误(抱歉所有JSON dat不确定是否包含): 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);
            }
        }
    }

我不确定导致此错误的原因,欢迎提出任何建议。如果需要,我可以提供更多代码。谢谢。

2 个答案:

答案 0 :(得分:2)

只想添加到eMad的答案中,这有助于我解决您遇到的同样问题。我希望这可以帮助任何追随我的人,因为这个小妞虫在我一天中的2个小时(或者夜晚,我是夜间活动)中丧生。嗯,进一步(ado?adieu?),你走了:P.S。下面的代码将进入你的私有类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();
    }

答案 1 :(得分:0)

我的一个朋友给我带来了一个与你的产生相同输出的代码。我认为这是您正在寻找的解决方案。给定代码是

// inside the class which connects to URL (Probably MainList)
    InputStream inputStream = connection.getInputStream();
    Reader reader = new InputStreamReader(inputStream);
    int contentLength = connection.getContentLength();
    char[] charArray = new char[contentLength];
    reader.read(charArray);
    String responseData = new String(charArray);

    jsonResponse = new JSONObject(responseData);

但是不知道为什么使用上面的代码,你要么没有获得完整的字符串,要么得到ContentLenght,但是最后几个字符没有被正确接收。请使用 following 代码来读取完整的回复:

    URLConnection yc = blogFeedUrl.openConnection();
    BufferedReader in = new BufferedReader(new InputStreamReader(
                                yc.getInputStream()));
    String inputLine;
    responseData = "";
    while ((inputLine = in.readLine()) != null) // read till you can receive any data
        responseData += inputLine;
    in.close();