我一直在一个应用程序从API获取书籍(目前硬编码)错误解析JSON的Android
这里是我的Java代码:
package com.example.vidit.books;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
public class Request extends AppCompatActivity {
public static final String LOG_TAG = Request.class.getSimpleName();
String response = "{\n"
+ "kind: \"books#volumes\",\n"
+ "totalItems: 1097,\n"
+ "items: [\n"
+ "{\n"
+ "kind: \"books#volume\",\n"
+ "id: \"MoXpe6H2B5gC\",\n"
+ "etag: \"9f/Qo6rf8Fs\",\n"
+ "selfLink: \"https://www.googleapis.com/books/v1/volumes/MoXpe6H2B5gC\",\n"
+ "volumeInfo: {\n"
+ "title: \"Android in The Attic\",\n"
+ "authors: [\n"
+ "\"Nicholas Allan\"\n"
+ "],\n"
+ "publisher: \"Hachette UK\",\n"
+ "publishedDate: \"2013-01-03\",\n"
+ "description: \"Aunt Edna has created a no-nonsense nanny android to make sure Billy and Alfie don't have any fun. But then Alfie discovers how to override Auntie Anne-Droid's programming and nothing can stop them eating all the Cheeki Choko Cherry Cakes they like ... until the real aunt Edna is kidnapped!\",\n"
+ "industryIdentifiers: [\n"
+ "{\n"
+ "type: \"ISBN_13\",\n"
+ "identifier: \"9781444905465\"\n"
+ "},\n"
+ "{\n"
+ "type: \"ISBN_10\",\n"
+ "identifier: \"1444905465\"\n"
+ "}\n"
+ "],\n"
+ "readingModes: {\n"
+ "text: true,\n"
+ "image: false\n"
+ "},\n"
+ "pageCount: 192,\n"
+ "printType: \"BOOK\",\n"
+ "categories: [\n"
+ "\"Juvenile Fiction\"\n"
+ "],\n"
+ "maturityRating: \"NOT_MATURE\",\n"
+ "allowAnonLogging: false,\n"
+ "contentVersion: \"1.2.2.0.preview.2\",\n"
+ "imageLinks: {\n"
+ "smallThumbnail: \"http://books.google.com/books/content?id=MoXpe6H2B5gC&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api\",\n"
+ "thumbnail: \"http://books.google.com/books/content?id=MoXpe6H2B5gC&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api\"\n"
+ "},\n"
+ "language: \"en\",\n"
+ "previewLink: \"http://books.google.co.in/books?id=MoXpe6H2B5gC&printsec=frontcover&dq=android&hl=&cd=1&source=gbs_api\",\n"
+ "infoLink: \"http://books.google.co.in/books?id=MoXpe6H2B5gC&dq=android&hl=&source=gbs_api\",\n"
+ "canonicalVolumeLink: \"http://books.google.co.in/books/about/Android_in_The_Attic.html?hl=&id=MoXpe6H2B5gC\"\n"
+ "},\n"
+ "saleInfo: {\n"
+ "country: \"IN\",\n"
+ "saleability: \"FOR_SALE\",\n"
+ "isEbook: true,\n"
+ "listPrice: {\n"
+ "amount: 212,\n"
+ "currencyCode: \"INR\"\n"
+ "},\n"
+ "retailPrice: {\n"
+ "amount: 148.4,\n"
+ "currencyCode: \"INR\"\n"
+ "},\n"
+ "buyLink: \"http://books.google.co.in/books?id=MoXpe6H2B5gC&dq=android&hl=&buy=&source=gbs_api\",\n"
+ "offers: [\n"
+ "{\n"
+ "finskyOfferType: 1,\n"
+ "listPrice: {\n"
+ "amountInMicros: 212000000,\n"
+ "currencyCode: \"INR\"\n"
+ "},\n"
+ "retailPrice: {\n"
+ "amountInMicros: 148400000,\n"
+ "currencyCode: \"INR\"\n"
+ "}\n"
+ "}\n"
+ "]\n"
+ "},\n"
+ "accessInfo: {\n"
+ "country: \"IN\",\n"
+ "viewability: \"PARTIAL\",\n"
+ "embeddable: true,\n"
+ "publicDomain: false,\n"
+ "textToSpeechPermission: \"ALLOWED_FOR_ACCESSIBILITY\",\n"
+ "epub: {\n"
+ "isAvailable: true,\n"
+ "acsTokenLink: \"http://books.google.co.in/books/download/Android_in_The_Attic-sample-epub.acsm?id=MoXpe6H2B5gC&format=epub&output=acs4_fulfillment_token&dl_type=sample&source=gbs_api\"\n"
+ "},\n"
+ "pdf: {\n"
+ "isAvailable: false\n"
+ "},\n"
+ "webReaderLink: \"http://books.google.co.in/books/reader?id=MoXpe6H2B5gC&hl=&printsec=frontcover&output=reader&source=gbs_api\",\n"
+ "accessViewStatus: \"SAMPLE\",\n"
+ "quoteSharingAllowed: false\n"
+ "},\n"
+ "searchInfo: {\n"
+ "textSnippet: \"Aunt Edna has created a no-nonsense nanny android to make sure Billy and Alfie don't have any fun.\"\n"
+ "}\n"
+ "},"
+ "}";
String APIURL;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_request);
ArrayList<Book> book = new ArrayList<Book>();
try {
book = extractJSON(response);
} catch (JSONException e) {
e.printStackTrace();
}
BookAdapter bookAdapter = new BookAdapter(this, book);
ListView listView = (ListView) findViewById(R.id.listview_all);
listView.setAdapter(bookAdapter);
Intent i = getIntent();
String text = i.getStringExtra("text");
APIURL = "https://www.googleapis.com/books/v1/volumes?q=android";
}
public static ArrayList<Book> extractJSON(String abc) throws JSONException {
ArrayList<Book> book = new ArrayList<Book>();
try {
JSONObject root = new JSONObject(abc);
JSONArray item = root.getJSONArray("items");
JSONObject name = item.getJSONObject(0);
String title = name.getString("title");
book.add(new Book(title, null));
} catch (JSONException e) {
// If an error is thrown when executing any of the above statements in the "try" block,
// catch the exception here, so the app doesn't crash. Print a log message
// with the message from the exception.
Log.e("QueryUtils", "Problem parsing the earthquake JSON results", e);
}
return book;
}
}
以下是我所遇到的误差修改当我的代码跑了。 logcat显示这些错误。 我不能明白的地方我错了
检查您的JSON是否验证...使用http://www.jsonlint.com – StackFlowed
您的json错误已满 – AndroidHacker
尝试使用一些在线json格式化程序:[freeformatter](http://www.freeformatter.com/ json-formatter.html)和[Jsonviewer](http://jsonviewer.stack.hu/)。用它来检查你的错误。 –