大家好,我是 Android 新手,正在使用网络 API。我目前正在编写一个应用程序,它可以扫描书中的条形码,然后在 Google 图书中搜索它。
到目前为止,我已将 Scandit 实现到我的应用程序中,我注册并从 Google API 控制台获取了 Books API 的 API key 。从那里我不知道如何继续并开始编码。到目前为止,据我所知,它需要我通过 uri 发出请求数据,但我一直在研究如何对其进行实际编码。我想知道是否有人可以指出正确的方向或提供示例代码来说明如何使用 URI 获取数据。
我还下载了 zipped Book API Jar libraries我需要利用这个吗?我问这个是因为从这个网站上关于 Google Places API 的问题中,答案之一是您需要的只是使用 Google API 作为构建目标,它不需要任何 Jar 文件,但这是否适用于 Books API好吗?
另外我正在使用 Eclipse,我应该将我的构建目标设置为 Google APIs 16 吗?我猜这是对的,因为我计划将来通过此应用使用 Google map 。
谢谢,这是我第一次在这里提问。
最佳答案
我刚刚自己完成了这个。这就是我使用 HttpURLConnection
和 AsyncTask
实现它的方式(我只是调用“https://www.googleapis.com/books/v1/volumes?q=isbn :”+yourISBN 并解析 JSON):
// Received ISBN from Barcode Scanner. Send to GoogleBooks to obtain book information.
class GoogleApiRequest extends AsyncTask<String, Object, JSONObject>{
@Override
protected void onPreExecute() {
// Check network connection.
if(isNetworkConnected() == false){
// Cancel request.
Log.i(getClass().getName(), "Not connected to the internet");
cancel(true);
return;
}
}
@Override
protected JSONObject doInBackground(String... isbns) {
// Stop if cancelled
if(isCancelled()){
return null;
}
String apiUrlString = "https://www.googleapis.com/books/v1/volumes?q=isbn:" + isbns[0];
try{
HttpURLConnection connection = null;
// Build Connection.
try{
URL url = new URL(apiUrlString);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setReadTimeout(5000); // 5 seconds
connection.setConnectTimeout(5000); // 5 seconds
} catch (MalformedURLException e) {
// Impossible: The only two URLs used in the app are taken from string resources.
e.printStackTrace();
} catch (ProtocolException e) {
// Impossible: "GET" is a perfectly valid request method.
e.printStackTrace();
}
int responseCode = connection.getResponseCode();
if(responseCode != 200){
Log.w(getClass().getName(), "GoogleBooksAPI request failed. Response Code: " + responseCode);
connection.disconnect();
return null;
}
// Read data from response.
StringBuilder builder = new StringBuilder();
BufferedReader responseReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line = responseReader.readLine();
while (line != null){
builder.append(line);
line = responseReader.readLine();
}
String responseString = builder.toString();
Log.d(getClass().getName(), "Response String: " + responseString);
JSONObject responseJson = new JSONObject(responseString);
// Close connection and return response code.
connection.disconnect();
return responseJson;
} catch (SocketTimeoutException e) {
Log.w(getClass().getName(), "Connection timed out. Returning null");
return null;
} catch(IOException e){
Log.d(getClass().getName(), "IOException when connecting to Google Books API.");
e.printStackTrace();
return null;
} catch (JSONException e) {
Log.d(getClass().getName(), "JSONException when connecting to Google Books API.");
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(JSONObject responseJson) {
if(isCancelled()){
// Request was cancelled due to no network connection.
showNetworkDialog();
} else if(responseJson == null){
showSimpleDialog(getResources().getString(R.string.dialog_null_response));
}
else{
// All went well. Do something with your new JSONObject.
}
}
}
protected boolean isNetworkConnected(){
// Instantiate mConnectivityManager if necessary
if(mConnectivityManager == null){
mConnectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
}
// Is device connected to the Internet?
NetworkInfo networkInfo = mConnectivityManager.getActiveNetworkInfo();
if(networkInfo != null && networkInfo.isConnected()){
return true;
} else {
return false;
}
}
我省略了我的对话框方法的代码,因为它们不相关。希望这会有所帮助。
关于android - 在 Android 中使用 Google Books API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14571478/