我是Android新手,正在使用web API,我正在编写一个Android应用程序,它可以扫描图书中的条形码,然后在Google Books API中搜索其ISBN。
在条形码扫描后,我有一个网址:https://www.googleapis.com/books/v1/volumes?q=isbn:9788432250651&AIzaSyCpYez5556X4UzPV6rF4kkspj9DsCs_Q_c
下面的代码是:
private class GetBookInfo extends AsyncTask <View, Void, Integer> {
@Override
protected Integer doInBackground(View... urls) {
// make Call to the url
makeCall("https://www.googleapis.com/books/v1/volumes?" +
"q=isbn:" + ean_content + "&AIzaSyCpYez5556X4UzPV6rF4kkspj9DsCs_Q_c");
//print the call in the console
System.out.println("https://www.googleapis.com/books/v1/volumes?" +
"q=isbn:" + ean_content + "&AIzaSyCpYez5556X4UzPV6rF4kkspj9DsCs_Q_c");
return null;
}
@Override
protected void onPreExecute() {
// we can start a progress bar here
}
@Override
protected void onPostExecute(Integer result) {
String ruta = save_cover(getApplicationContext(), title, book_cover);
Intent intent = new Intent(MainActivity.this, Spreadsheets.class);
// intent.putExtra(title,title);
// intent.putExtra(author,authors);
// intent.putExtra(date,date);
// intent.putExtra(category,categories);
// intent.putExtra(description,description);
//finish();
startActivity(intent);
finish();
}
}
public void makeCall(String stringURL) {
URL url = null;
BufferedInputStream is = null;
JsonReader jsonReader;
try {
url = new URL(stringURL);
} catch (Exception ex) {
System.out.println("Malformed URL");
}
try {
if (url != null) {
HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
is = new BufferedInputStream(urlConnection.getInputStream());
}
} catch (IOException ioe) {
System.out.println("IOException");
}
if (is != null) {
try {
jsonReader = new JsonReader(new InputStreamReader(is, "UTF-8"));
jsonReader.beginObject();
while (jsonReader.hasNext()) {
String name = jsonReader.nextName();
if (name.equals("title")) {
title = jsonReader.nextString();
}
else if (name.equals("authors")) {
authors = jsonReader.nextString();
}
else if (name.equals("publishedDate")) {
date = jsonReader.nextString();
}
else if (name.equals("categories")) {
categories = jsonReader.nextString();
}
else if (name.equals("description")) {
description = jsonReader.nextString();
}
// else if (name.equals("averageRating")) {
// rating = jsonReader.nextString();
// }
else if (name.equals("thumbnail")) {
image = jsonReader.nextString();
book_cover = download_cover(image);
}
else {
jsonReader.skipValue();
}
}
jsonReader.endObject();
}
catch (Exception e) {
System.out.println("Exception");
}
}
}
这不是从API中检索任何内容。非常感谢您的帮助,谢谢!
发布于 2017-07-31 13:26:17
我认为您下一步需要做的是从API请求一个连接,打开该连接,使用JSON从API检索数据,并使用inputStream获取存储在数组中的数据。类似于:在类中实现这些方法:
私有静态字符串makeHttpRequest( url )抛出IOException
私有静态字符串readFromStream(InputStream inputStream)引发IOException
私有静态列表extractFeatureFromJson(String booksJson)
公共静态列表featchBookData(String requestUrl)
发布于 2017-11-29 04:01:23
Here是一个完整的代码示例,展示了如何在Android中通过Feign或Retrofit使用Google Books API。这些库在HTTP之上提供了更高级别的抽象,这样您就可以在代码中使用简单的方法调用和对象,而不是处理请求、响应和JSON反序列化。
https://stackoverflow.com/questions/45309061
复制相似问题