我用customarrayadapter创建了arraylist,在使用模拟数据时,列表在屏幕上是可见的,没有任何错误,但是当我用jsonobject替换模拟数据时,json响应在java文件中被硬编码为字符串值,然后我发现所有数据都正确地显示在列表中,但是只有1个列表项显示在屏幕上,而不是15个,因为响应包含15个jsonarray对象。我使用for循环遍历jsonarray,但是我不知道错误是什么?
mainactivity.java:
package com.example.anybookinfo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView bookListView = findViewById(R.id.bookListView);
ArrayList<BookStore> bookStoreArrayList = HTTTP_REQUEST.readFromJson(HTTTP_REQUEST.jsonResponse);
BookAdapter bookAdapter = new BookAdapter(this, bookStoreArrayList);
bookListView.setAdapter(bookAdapter);
}
}
http\ U请求.java:
package com.example.anybookinfo;
import android.text.TextUtils;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
public final class HTTTP_REQUEST {
public static ArrayList<BookStore> readFromJson(String jsonObject) {
ArrayList<BookStore> currentBookStore = new ArrayList<>();
if (TextUtils.isEmpty(jsonObject)) {
return null;
}
try {
JSONObject root = new JSONObject(jsonObject);
JSONArray items = root.getJSONArray("items");
for (int i = 0; i < items.length(); i++) {
JSONObject currentBook = items.getJSONObject(i);
JSONObject volumeInfo = currentBook.getJSONObject("volumeInfo");
String fTitle = volumeInfo.getString("title");
String lTitle = volumeInfo.getString("subtitle");
JSONArray author = volumeInfo.getJSONArray("authors");
String authorName = author.getString(0);
int PageCount = volumeInfo.getInt("pageCount");
JSONObject saleInfo = currentBook.getJSONObject("saleInfo");
JSONObject listPrice = saleInfo.getJSONObject("listPrice");
double amount = listPrice.getDouble("amount");
String currencyCode = listPrice.getString("currencyCode");
JSONObject searchInfo = currentBook.getJSONObject("searchInfo");
String textSnippet = searchInfo.getString("textSnippet");
BookStore bookStore = new BookStore(fTitle, lTitle, authorName, textSnippet, PageCount, amount, currencyCode);
currentBookStore.add(bookStore);
}
} catch (JSONException j) {
Log.e("readFromJson Error", "error in reading the json response", j);
}
return currentBookStore;
}
public static final String jsonResponse = " //the json response code is here " ;
}
硬编码的实际json响应链接:https://www.googleapis.com/books/v1/volumes?q=inner%20engineering%20用法:%20a%20yogi%27s%20guide%20to%20joy&maxresults=15
logcat错误:
01-24 23:18:46.030 3693-3703/? E/art: Failed sending reply to debugger: Broken pipe
01-24 23:18:46.243 3693-3693/? E/readFromJson Error: error in reading the json response
org.json.JSONException: No value for listPrice
at org.json.JSONObject.get(JSONObject.java:389)
com.example.anybookinfo.HTTTP_REQUEST.readFromJson(HTTTP_REQUEST.java:58)
at com.example.anybookinfo.MainActivity.onCreate(MainActivity.java:18)
主要活动。java:18 line 包含:
ArrayList<BookStore> bookStoreArrayList = HTTTP_REQUEST.readFromJson(HTTTP_REQUEST.jsonResponse);
htttp\u请求。java:58 line 包含:
JSONObject listPrice = saleInfo.getJSONObject("listPrice");
2条答案
按热度按时间eblbsuwk1#
谢谢https://stackoverflow.com/users/3851567/david-velasquez 寻找答案。谢谢你https://stackoverflow.com/users/2637449/md-asaduzzaman 指出使用opt而不是get的实际错误。在修复了这个错误之后,我发现了更多类似的错误,没有赋值,我使用optstring、optobject代替getstring、getjson,并在缺少值时指定了一个默认值。
以下是我在readfromjson方法中为解决错误所做的更改:
zvms9eto2#
感谢您发布logcat错误。这就是您的项目没有显示在屏幕上的原因。在查看了您提供的json数据之后,您需要对
listPrice
在处理它之前,因为saleInfo
没有那个领域。所以不是这句话:JSONObject listPrice = saleInfo.getJSONObject("listPrice");
执行:JSONObject listPrice = saleInfo.optJSONObject("listPrice");
然后对listPrice
:不需要再添加一个内部try/catch。另外,你需要使用
Double
类,因为存在空值的可能性,并且Bookstore
构造函数正在传递该字段。如果有其他字段可能不在json响应中,那么您也需要在这些jsonobjects上添加null检查。