gpt4 book ai didi

java - JSONException : No value for book_details

转载 作者:行者123 更新时间:2023-12-01 10:27:23 25 4
gpt4 key购买 nike

我正在尝试从此网址中抓取书名:http://api.nytimes.com/svc/books/v2/lists/2010-10-01/trade-fiction-paperback?api-key=c775dd98c5a108e9ebc790ca2c47ac19:18:74338855

我使用以下代码从该网址中抓取信息,该代码当时有效:http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=44db6a862fba0b067b1930da0d769e98

它适用于第二个网址,因为我只抓取了一个“天气”字符串。然而,在另一个网址中有几个“book_details”字符串,我不知道如何从每个字符串中抓取标题。

这是我的代码,返回 JSONException: No value for book_details。

public class MainActivity extends AppCompatActivity {

public class DownloadTask extends AsyncTask<String, Void, String>{

@Override
protected String doInBackground(String... urls) {

String result = "";
URL url;
HttpURLConnection urlConnection = null;

try {

url = new URL(urls[0]);

urlConnection = (HttpURLConnection)url.openConnection();

InputStream in = urlConnection.getInputStream();

InputStreamReader reader = new InputStreamReader(in);

int data = reader.read();

while (data != -1){

char current = (char) data;

result += current;

data = reader.read();

}

return result;

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return null;

}

@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);

try {

JSONObject jsonObject = new JSONObject(result);

String bookInfo = jsonObject.getString("book_details");

Log.i("Book Info", bookInfo);


} catch (JSONException e) {

e.printStackTrace();

}


}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

DownloadTask task = new DownloadTask();
task.execute("http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=44db6a862fba0b067b1930da0d769e98");

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

我认为问题出在 onPostExecute 方法中。任何帮助将不胜感激。

最佳答案

您的代码的一个大问题是您没有正确循环结果。您收到的是每本书的 JSONObjects JSONArray,因此您需要正确循环每个数组。下面是一个基本示例,说明了提取名称和描述。从那里您确实可以获取任何您想要的信息。不过,我强烈建议学习如何在 Android 中解析 JSONObjects 和数组。

 try {

JSONObject jsonObject = new JSONObject(result);
JSONArray jsonArray = jsonObject.getJSONArray("results");

for(int i = 0; i < jsonArray.length(); i++) {
JSONObject currentObject = jsonArray.getJSONObject(i);
JSONArray currentArray = currentObject.getJSONArray("book_details");
for(int x = 0; x < currentArray.length(); x++){
JSONObject book = currentArray.getJSONObject(x);
String bookTitle = book.getString("title");
String bookDescription = book.getString("description");
Log.i("Book Title: ", bookTitle);
Log.i("Book Description: ", bookDescription);
}
}
}catch(JSONException e){
e.printStackTrace();
}

输出

enter image description here

其他资源

http://developer.android.com/reference/org/json/JSONObject.html

http://www.tutorialspoint.com/android/android_json_parser.htm

http://www.vogella.com/tutorials/AndroidJSON/article.html

关于java - JSONException : No value for book_details,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35293670/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com