2017-06-13 8 views
0

APIの使い方が新しく、GoogleブックスAPIを使用して簡単なアプリケーションを作成していますので、タイトル、著者、説明。これは情報を抽出するメソッドですが、例外をスローしてcatchブロックを実行します。GoogleブックAPIからデータを取得しようとするとエラーが発生します

private static List<Book> extractFeatureFromJson(String bookJSON) { 

     if (TextUtils.isEmpty(bookJSON)) { 
      return null; 
     } 

        List<Book> books = new ArrayList<>(); 


     try { 

      // Create a JSONObject from the JSON response string 
      JSONObject baseJsonResponse = new JSONObject(bookJSON); 


      JSONArray itemsArray = baseJsonResponse.getJSONArray("items"); 


      for (int i = 0; i < itemsArray.length(); i++) { 

          JSONObject currentBook = itemsArray.getJSONObject(i); 

       JSONArray authorArray = currentBook.getJSONArray("authors"); 

       StringBuilder authorsStringBuild = new StringBuilder(); 

       for(int j=0; j<authorArray.length(); j++){ 
        authorsStringBuild.append(authorArray.getString(j)); 
        authorsStringBuild.append(", "); 
       } 

       String authors = authorsStringBuild.toString(); 

       String title = currentBook.getString("title"); 

       String description = currentBook.getString("description"); 

       double rating = currentBook.getDouble("averageRating"); 

       Book book = new Book(title, authors ,description , rating); 

       books.add(book); 
      } 

     } catch (JSONException e) { 

      Log.e(LOG_TAG, "Problem parsing the book JSON results", e); 
     } 

     return books; 
    } 

答えて

0

将来的に出力し、エラーログを投稿し行ってください:これはbookJSON変数は、クエリ応答であるクエリ「https://www.googleapis.com/books/v1/volumes?q=android&maxResults=1」です。私はそれについてコメントするほどの評判はなかった。 コードを実行しましたが、JSONの解析エラーです。 "作者" の前に、この

itemsArray.getJSONObject(i).getJSONObject("volumeInfo").getJSONArray("authors"); 

あなたがJSONobjectを欠落していたとあなたのコード

JSONArray authorArray = currentBook.getJSONArray("authors"); 

でこの行を置き換え 。

オンラインJSONフォーマットツールを使用すると、階層をよりよく視覚化してより簡単に使用できます。これは私が好きなものですhttps://jsonformatter.curiousconcept.com/

+0

ありがとう、それは完全に働いた –

+0

あなたは大歓迎です! :) –

関連する問題