首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从Google Books API检索数据

从Google Books API检索数据
EN

Stack Overflow用户
提问于 2017-07-26 00:35:49
回答 2查看 2.2K关注 0票数 1

我是Android新手,正在使用web API,我正在编写一个Android应用程序,它可以扫描图书中的条形码,然后在Google Books API中搜索其ISBN。

在条形码扫描后,我有一个网址:https://www.googleapis.com/books/v1/volumes?q=isbn:9788432250651&AIzaSyCpYez5556X4UzPV6rF4kkspj9DsCs_Q_c

下面的代码是:

代码语言:javascript
运行
复制
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中检索任何内容。非常感谢您的帮助,谢谢!

EN

回答 2

Stack Overflow用户

发布于 2017-07-31 13:26:17

我认为您下一步需要做的是从API请求一个连接,打开该连接,使用JSON从API检索数据,并使用inputStream获取存储在数组中的数据。类似于:在类中实现这些方法:

私有静态字符串makeHttpRequest( url )抛出IOException

私有静态字符串readFromStream(InputStream inputStream)引发IOException

私有静态列表extractFeatureFromJson(String booksJson)

公共静态列表featchBookData(String requestUrl)

票数 0
EN

Stack Overflow用户

发布于 2017-11-29 04:01:23

Here是一个完整的代码示例,展示了如何在Android中通过Feign或Retrofit使用Google Books API。这些库在HTTP之上提供了更高级别的抽象,这样您就可以在代码中使用简单的方法调用和对象,而不是处理请求、响应和JSON反序列化。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45309061

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档