android在将json对象从url解析到listview时遇到nullpoint异常
我试图解析json对象以将其填充到列表视图中,但我遇到了一些问题,请提供帮助android在将json对象从url解析到listview时遇到nullpoint异常,android,listview,android-arrayadapter,json,Android,Listview,Android Arrayadapter,Json,我试图解析json对象以将其填充到列表视图中,但我遇到了一些问题,请提供帮助 JSONParser jParser = new JSONParser(); // getting Json String from url JSONArray json = jParser.getJSONFromUrl(url); // getting array of books //Get ri
JSONParser jParser = new JSONParser();
// getting Json String from url
JSONArray json = jParser.getJSONFromUrl(url);
// getting array of books
//Get rid of this = book = json.getJSONArray(TAG_BOOK);
// looping through all books
for(int i=0;i<json.length();i++){
try{
JSONObject parent = json.getJSONObject(i);
JSONObject c = parent.optJSONObject("camera");
if(c == null)
c = parent.optJSONObject("book");
if(c == null)
c = parent.optJSONObject("music");
// storing each json item in variable
String title = c.getString(TAG_TITLE);
String price = c.getString(TAG_PRICE);
String authors = c.getString(TAG_AUTHORS);
String id = c.getString(TAG_ID);
String description = c.getString(TAG_DESCRIPTION);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_ID, id);
map.put(TAG_TITLE, title);
map.put(TAG_PRICE, price);
map.put(TAG_AUTHORS, authors);
map.put(TAG_DESCRIPTION, description);
bookList.add(map);
}
catch (JSONException e) {
e.printStackTrace();
}
}
我得到了NullpointerException
JSONParser jParser = new JSONParser();
// getting Json String from url
JSONArray json = jParser.getJSONFromUrl(url);
// getting array of books
//Get rid of this = book = json.getJSONArray(TAG_BOOK);
// looping through all books
for(int i=0;i<json.length();i++){
try{
JSONObject parent = json.getJSONObject(i);
JSONObject c = parent.optJSONObject("camera");
if(c == null)
c = parent.optJSONObject("book");
if(c == null)
c = parent.optJSONObject("music");
// storing each json item in variable
String title = c.getString(TAG_TITLE);
String price = c.getString(TAG_PRICE);
String authors = c.getString(TAG_AUTHORS);
String id = c.getString(TAG_ID);
String description = c.getString(TAG_DESCRIPTION);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_ID, id);
map.put(TAG_TITLE, title);
map.put(TAG_PRICE, price);
map.put(TAG_AUTHORS, authors);
map.put(TAG_DESCRIPTION, description);
bookList.add(map);
}
catch (JSONException e) {
e.printStackTrace();
}
}
这是我的全部代码:
JSONParser jParser = new JSONParser();
// getting Json String from url
JSONArray json = jParser.getJSONFromUrl(url);
// getting array of books
//Get rid of this = book = json.getJSONArray(TAG_BOOK);
// looping through all books
for(int i=0;i<json.length();i++){
try{
JSONObject parent = json.getJSONObject(i);
JSONObject c = parent.optJSONObject("camera");
if(c == null)
c = parent.optJSONObject("book");
if(c == null)
c = parent.optJSONObject("music");
// storing each json item in variable
String title = c.getString(TAG_TITLE);
String price = c.getString(TAG_PRICE);
String authors = c.getString(TAG_AUTHORS);
String id = c.getString(TAG_ID);
String description = c.getString(TAG_DESCRIPTION);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_ID, id);
map.put(TAG_TITLE, title);
map.put(TAG_PRICE, price);
map.put(TAG_AUTHORS, authors);
map.put(TAG_DESCRIPTION, description);
bookList.add(map);
}
catch (JSONException e) {
e.printStackTrace();
}
}
我正在解析来自以下url的数据
JSONParser jParser = new JSONParser();
// getting Json String from url
JSONArray json = jParser.getJSONFromUrl(url);
// getting array of books
//Get rid of this = book = json.getJSONArray(TAG_BOOK);
// looping through all books
for(int i=0;i<json.length();i++){
try{
JSONObject parent = json.getJSONObject(i);
JSONObject c = parent.optJSONObject("camera");
if(c == null)
c = parent.optJSONObject("book");
if(c == null)
c = parent.optJSONObject("music");
// storing each json item in variable
String title = c.getString(TAG_TITLE);
String price = c.getString(TAG_PRICE);
String authors = c.getString(TAG_AUTHORS);
String id = c.getString(TAG_ID);
String description = c.getString(TAG_DESCRIPTION);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_ID, id);
map.put(TAG_TITLE, title);
map.put(TAG_PRICE, price);
map.put(TAG_AUTHORS, authors);
map.put(TAG_DESCRIPTION, description);
bookList.add(map);
}
catch (JSONException e) {
e.printStackTrace();
}
}
JSONParser jParser = new JSONParser();
// getting Json String from url
JSONArray json = jParser.getJSONFromUrl(url);
// getting array of books
//Get rid of this = book = json.getJSONArray(TAG_BOOK);
// looping through all books
for(int i=0;i<json.length();i++){
try{
JSONObject parent = json.getJSONObject(i);
JSONObject c = parent.optJSONObject("camera");
if(c == null)
c = parent.optJSONObject("book");
if(c == null)
c = parent.optJSONObject("music");
// storing each json item in variable
String title = c.getString(TAG_TITLE);
String price = c.getString(TAG_PRICE);
String authors = c.getString(TAG_AUTHORS);
String id = c.getString(TAG_ID);
String description = c.getString(TAG_DESCRIPTION);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_ID, id);
map.put(TAG_TITLE, title);
map.put(TAG_PRICE, price);
map.put(TAG_AUTHORS, authors);
map.put(TAG_DESCRIPTION, description);
bookList.add(map);
}
catch (JSONException e) {
e.printStackTrace();
}
}
解析数据的代码
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
public JSONParser(){
}
public JSONObject getJSONFromUrl(String url){
//making Http request
try{
//default httpclient
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 parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
JSONParser jParser = new JSONParser();
// getting Json String from url
JSONArray json = jParser.getJSONFromUrl(url);
// getting array of books
//Get rid of this = book = json.getJSONArray(TAG_BOOK);
// looping through all books
for(int i=0;i<json.length();i++){
try{
JSONObject parent = json.getJSONObject(i);
JSONObject c = parent.optJSONObject("camera");
if(c == null)
c = parent.optJSONObject("book");
if(c == null)
c = parent.optJSONObject("music");
// storing each json item in variable
String title = c.getString(TAG_TITLE);
String price = c.getString(TAG_PRICE);
String authors = c.getString(TAG_AUTHORS);
String id = c.getString(TAG_ID);
String description = c.getString(TAG_DESCRIPTION);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_ID, id);
map.put(TAG_TITLE, title);
map.put(TAG_PRICE, price);
map.put(TAG_AUTHORS, authors);
map.put(TAG_DESCRIPTION, description);
bookList.add(map);
}
catch (JSONException e) {
e.printStackTrace();
}
}
}
JSONParser jParser = new JSONParser();
// getting Json String from url
JSONArray json = jParser.getJSONFromUrl(url);
// getting array of books
//Get rid of this = book = json.getJSONArray(TAG_BOOK);
// looping through all books
for(int i=0;i<json.length();i++){
try{
JSONObject parent = json.getJSONObject(i);
JSONObject c = parent.optJSONObject("camera");
if(c == null)
c = parent.optJSONObject("book");
if(c == null)
c = parent.optJSONObject("music");
// storing each json item in variable
String title = c.getString(TAG_TITLE);
String price = c.getString(TAG_PRICE);
String authors = c.getString(TAG_AUTHORS);
String id = c.getString(TAG_ID);
String description = c.getString(TAG_DESCRIPTION);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_ID, id);
map.put(TAG_TITLE, title);
map.put(TAG_PRICE, price);
map.put(TAG_AUTHORS, authors);
map.put(TAG_DESCRIPTION, description);
bookList.add(map);
}
catch (JSONException e) {
e.printStackTrace();
}
}
您的问题在于JSONParser。这和从站点返回的JSON格式不正确。您有一组JSONObject,其中JSONObject具有相似的名称。我做了一个快速简单的解决方案来处理它,但是如果您控制生成JSON的代码,那么我建议将项目类别(书籍、音乐、相机)分组到它们自己的JSONArray中
JSONParser jParser = new JSONParser();
// getting Json String from url
JSONArray json = jParser.getJSONFromUrl(url);
// getting array of books
//Get rid of this = book = json.getJSONArray(TAG_BOOK);
// looping through all books
for(int i=0;i<json.length();i++){
try{
JSONObject parent = json.getJSONObject(i);
JSONObject c = parent.optJSONObject("camera");
if(c == null)
c = parent.optJSONObject("book");
if(c == null)
c = parent.optJSONObject("music");
// storing each json item in variable
String title = c.getString(TAG_TITLE);
String price = c.getString(TAG_PRICE);
String authors = c.getString(TAG_AUTHORS);
String id = c.getString(TAG_ID);
String description = c.getString(TAG_DESCRIPTION);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_ID, id);
map.put(TAG_TITLE, title);
map.put(TAG_PRICE, price);
map.put(TAG_AUTHORS, authors);
map.put(TAG_DESCRIPTION, description);
bookList.add(map);
}
catch (JSONException e) {
e.printStackTrace();
}
}
您从网站返回的JSON是一个带有JSONObject的JSONArray。。你在做下面的事情
JSONParser jParser = new JSONParser();
// getting Json String from url
JSONArray json = jParser.getJSONFromUrl(url);
// getting array of books
//Get rid of this = book = json.getJSONArray(TAG_BOOK);
// looping through all books
for(int i=0;i<json.length();i++){
try{
JSONObject parent = json.getJSONObject(i);
JSONObject c = parent.optJSONObject("camera");
if(c == null)
c = parent.optJSONObject("book");
if(c == null)
c = parent.optJSONObject("music");
// storing each json item in variable
String title = c.getString(TAG_TITLE);
String price = c.getString(TAG_PRICE);
String authors = c.getString(TAG_AUTHORS);
String id = c.getString(TAG_ID);
String description = c.getString(TAG_DESCRIPTION);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_ID, id);
map.put(TAG_TITLE, title);
map.put(TAG_PRICE, price);
map.put(TAG_AUTHORS, authors);
map.put(TAG_DESCRIPTION, description);
bookList.add(map);
}
catch (JSONException e) {
e.printStackTrace();
}
}
jObj=新的JSONObject(json)
JSONParser jParser = new JSONParser();
// getting Json String from url
JSONArray json = jParser.getJSONFromUrl(url);
// getting array of books
//Get rid of this = book = json.getJSONArray(TAG_BOOK);
// looping through all books
for(int i=0;i<json.length();i++){
try{
JSONObject parent = json.getJSONObject(i);
JSONObject c = parent.optJSONObject("camera");
if(c == null)
c = parent.optJSONObject("book");
if(c == null)
c = parent.optJSONObject("music");
// storing each json item in variable
String title = c.getString(TAG_TITLE);
String price = c.getString(TAG_PRICE);
String authors = c.getString(TAG_AUTHORS);
String id = c.getString(TAG_ID);
String description = c.getString(TAG_DESCRIPTION);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_ID, id);
map.put(TAG_TITLE, title);
map.put(TAG_PRICE, price);
map.put(TAG_AUTHORS, authors);
map.put(TAG_DESCRIPTION, description);
bookList.add(map);
}
catch (JSONException e) {
e.printStackTrace();
}
}
你应该做什么
JSONParser jParser = new JSONParser();
// getting Json String from url
JSONArray json = jParser.getJSONFromUrl(url);
// getting array of books
//Get rid of this = book = json.getJSONArray(TAG_BOOK);
// looping through all books
for(int i=0;i<json.length();i++){
try{
JSONObject parent = json.getJSONObject(i);
JSONObject c = parent.optJSONObject("camera");
if(c == null)
c = parent.optJSONObject("book");
if(c == null)
c = parent.optJSONObject("music");
// storing each json item in variable
String title = c.getString(TAG_TITLE);
String price = c.getString(TAG_PRICE);
String authors = c.getString(TAG_AUTHORS);
String id = c.getString(TAG_ID);
String description = c.getString(TAG_DESCRIPTION);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_ID, id);
map.put(TAG_TITLE, title);
map.put(TAG_PRICE, price);
map.put(TAG_AUTHORS, authors);
map.put(TAG_DESCRIPTION, description);
bookList.add(map);
}
catch (JSONException e) {
e.printStackTrace();
}
}
jObj=新的JSONArray(json)
JSONParser jParser = new JSONParser();
// getting Json String from url
JSONArray json = jParser.getJSONFromUrl(url);
// getting array of books
//Get rid of this = book = json.getJSONArray(TAG_BOOK);
// looping through all books
for(int i=0;i<json.length();i++){
try{
JSONObject parent = json.getJSONObject(i);
JSONObject c = parent.optJSONObject("camera");
if(c == null)
c = parent.optJSONObject("book");
if(c == null)
c = parent.optJSONObject("music");
// storing each json item in variable
String title = c.getString(TAG_TITLE);
String price = c.getString(TAG_PRICE);
String authors = c.getString(TAG_AUTHORS);
String id = c.getString(TAG_ID);
String description = c.getString(TAG_DESCRIPTION);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_ID, id);
map.put(TAG_TITLE, title);
map.put(TAG_PRICE, price);
map.put(TAG_AUTHORS, authors);
map.put(TAG_DESCRIPTION, description);
bookList.add(map);
}
catch (JSONException e) {
e.printStackTrace();
}
}
然后,您需要将返回值更改为JSONArray,而不是JSONObject
JSONParser jParser = new JSONParser();
// getting Json String from url
JSONArray json = jParser.getJSONFromUrl(url);
// getting array of books
//Get rid of this = book = json.getJSONArray(TAG_BOOK);
// looping through all books
for(int i=0;i<json.length();i++){
try{
JSONObject parent = json.getJSONObject(i);
JSONObject c = parent.optJSONObject("camera");
if(c == null)
c = parent.optJSONObject("book");
if(c == null)
c = parent.optJSONObject("music");
// storing each json item in variable
String title = c.getString(TAG_TITLE);
String price = c.getString(TAG_PRICE);
String authors = c.getString(TAG_AUTHORS);
String id = c.getString(TAG_ID);
String description = c.getString(TAG_DESCRIPTION);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_ID, id);
map.put(TAG_TITLE, title);
map.put(TAG_PRICE, price);
map.put(TAG_AUTHORS, authors);
map.put(TAG_DESCRIPTION, description);
bookList.add(map);
}
catch (JSONException e) {
e.printStackTrace();
}
}
然后在主活动中将“json”更改为JSONArray并去掉“book”。只需循环JSONArray即可
JSONParser jParser = new JSONParser();
// getting Json String from url
JSONArray json = jParser.getJSONFromUrl(url);
// getting array of books
//Get rid of this = book = json.getJSONArray(TAG_BOOK);
// looping through all books
for(int i=0;i<json.length();i++){
try{
JSONObject parent = json.getJSONObject(i);
JSONObject c = parent.optJSONObject("camera");
if(c == null)
c = parent.optJSONObject("book");
if(c == null)
c = parent.optJSONObject("music");
// storing each json item in variable
String title = c.getString(TAG_TITLE);
String price = c.getString(TAG_PRICE);
String authors = c.getString(TAG_AUTHORS);
String id = c.getString(TAG_ID);
String description = c.getString(TAG_DESCRIPTION);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_ID, id);
map.put(TAG_TITLE, title);
map.put(TAG_PRICE, price);
map.put(TAG_AUTHORS, authors);
map.put(TAG_DESCRIPTION, description);
bookList.add(map);
}
catch (JSONException e) {
e.printStackTrace();
}
}
同时移动你的手臂,尝试进入循环
JSONParser jParser = new JSONParser();
// getting Json String from url
JSONArray json = jParser.getJSONFromUrl(url);
// getting array of books
//Get rid of this = book = json.getJSONArray(TAG_BOOK);
// looping through all books
for(int i=0;i<json.length();i++){
try{
JSONObject parent = json.getJSONObject(i);
JSONObject c = parent.optJSONObject("camera");
if(c == null)
c = parent.optJSONObject("book");
if(c == null)
c = parent.optJSONObject("music");
// storing each json item in variable
String title = c.getString(TAG_TITLE);
String price = c.getString(TAG_PRICE);
String authors = c.getString(TAG_AUTHORS);
String id = c.getString(TAG_ID);
String description = c.getString(TAG_DESCRIPTION);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_ID, id);
map.put(TAG_TITLE, title);
map.put(TAG_PRICE, price);
map.put(TAG_AUTHORS, authors);
map.put(TAG_DESCRIPTION, description);
bookList.add(map);
}
catch (JSONException e) {
e.printStackTrace();
}
}
通过这种方式,您可以对不同的类别执行不同的操作(如果需要),并将类别中的任何类似JSON对象分组在一起
JSONParser jParser = new JSONParser();
// getting Json String from url
JSONArray json = jParser.getJSONFromUrl(url);
// getting array of books
//Get rid of this = book = json.getJSONArray(TAG_BOOK);
// looping through all books
for(int i=0;i<json.length();i++){
try{
JSONObject parent = json.getJSONObject(i);
JSONObject c = parent.optJSONObject("camera");
if(c == null)
c = parent.optJSONObject("book");
if(c == null)
c = parent.optJSONObject("music");
// storing each json item in variable
String title = c.getString(TAG_TITLE);
String price = c.getString(TAG_PRICE);
String authors = c.getString(TAG_AUTHORS);
String id = c.getString(TAG_ID);
String description = c.getString(TAG_DESCRIPTION);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_ID, id);
map.put(TAG_TITLE, title);
map.put(TAG_PRICE, price);
map.put(TAG_AUTHORS, authors);
map.put(TAG_DESCRIPTION, description);
bookList.add(map);
}
catch (JSONException e) {
e.printStackTrace();
}
}
或者,如果无论是书籍、音乐还是相机,操作都是相同的,那么您可以在JSON对象中添加另一个字段作为“type”,然后在数组(JSON_array.put())中创建JSONObject,而不使用名称,使其显示为
JSONParser jParser = new JSONParser();
// getting Json String from url
JSONArray json = jParser.getJSONFromUrl(url);
// getting array of books
//Get rid of this = book = json.getJSONArray(TAG_BOOK);
// looping through all books
for(int i=0;i<json.length();i++){
try{
JSONObject parent = json.getJSONObject(i);
JSONObject c = parent.optJSONObject("camera");
if(c == null)
c = parent.optJSONObject("book");
if(c == null)
c = parent.optJSONObject("music");
// storing each json item in variable
String title = c.getString(TAG_TITLE);
String price = c.getString(TAG_PRICE);
String authors = c.getString(TAG_AUTHORS);
String id = c.getString(TAG_ID);
String description = c.getString(TAG_DESCRIPTION);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_ID, id);
map.put(TAG_TITLE, title);
map.put(TAG_PRICE, price);
map.put(TAG_AUTHORS, authors);
map.put(TAG_DESCRIPTION, description);
bookList.add(map);
}
catch (JSONException e) {
e.printStackTrace();
}
}
[
{
"type":"camera",
"picture":"http://img5.flixcart.com/image/camera/5/p/a/nikon-d90-slr-40x40-imacxmh3y6wmqh8b.jpeg",
"model":"D90 SLR with AF-S 18-105mm VR Kit Lens",
"make":"Nikon",
"price":57182.0
},
{
"type":"book",
"description":"This book is one of a series of texts written by faculty of the Electrical Engineering and Computer Science Department at the Massachusetts Institute of Technology. It was edited and produced by The MIT Press under a joint production-distribution arrangement with the McGraw-Hill Book Company.",
"authors":"Harold Abelson and Gerald Jay Sussman with Julie Sussman",
"price":469.0,
"id":51087,
"title":"Structure and Interpretation of Computer Programs"
}
]
再说一遍,您应该采取的路线取决于您以后将如何处理数据请发布LogCat错误,如果您可以读取LogCat,请指出发生NullPointerException的行。AndroidJSONParsingActivity.java中的第62行是什么?是的,您的错误率为100%,现在我没有遇到任何问题,输出是正确的正确,请你建议我将项目类别(书籍、音乐、相机)分组到各自的JSONArray中。创建一个主JSONArray。然后为每个类别创建JSONArray(即,一个用于音乐,一个用于书籍,一个用于相机)。您将使用每个JSONObject(标题、价格等)填充Category JSONArray。将所有项目添加到类别数组后,将每个类别数组作为JSON对象添加到主数组中。我编辑了我的答案,以显示我仍在尝试的格式,但你的答案对我帮助很大,我正在尝试一份工作,这是分配给我的作业。谢谢。我在哪里可以点击告诉你这个问题的答案
JSONParser jParser = new JSONParser();
// getting Json String from url
JSONArray json = jParser.getJSONFromUrl(url);
// getting array of books
//Get rid of this = book = json.getJSONArray(TAG_BOOK);
// looping through all books
for(int i=0;i<json.length();i++){
try{
JSONObject parent = json.getJSONObject(i);
JSONObject c = parent.optJSONObject("camera");
if(c == null)
c = parent.optJSONObject("book");
if(c == null)
c = parent.optJSONObject("music");
// storing each json item in variable
String title = c.getString(TAG_TITLE);
String price = c.getString(TAG_PRICE);
String authors = c.getString(TAG_AUTHORS);
String id = c.getString(TAG_ID);
String description = c.getString(TAG_DESCRIPTION);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_ID, id);
map.put(TAG_TITLE, title);
map.put(TAG_PRICE, price);
map.put(TAG_AUTHORS, authors);
map.put(TAG_DESCRIPTION, description);
bookList.add(map);
}
catch (JSONException e) {
e.printStackTrace();
}
}