use*_*478 4 android google-api google-books
嗨,我是Android新手并使用Web API.我目前正在编写一个应用程序,可以扫描书籍中的条形码,然后搜索Google Books.
到目前为止,我已将Scandit应用到我的应用程序中,并注册并从Google API控制台获取了用于Books API的API密钥.从那里我不知道如何继续并开始编码.到目前为止,根据我的理解,它需要我通过uri发出请求数据,但我仍然坚持如何实际编码它.我想知道是否有人能指出我正确的方向或提供一个示例代码,说明如何使用URI获取数据.
我还下载了压缩的Book API Jar库我需要使用它吗?我问这个,因为从这个网站在谷歌Places API的一个问题,答案中的一个说,所有你需要的是使用谷歌API作为构建目标,它不需要任何.jar文件,但是这并不适用于书籍作为API好?
我也在使用Eclipse,我应该将构建目标设置为Google API 16吗?我猜这是对的,因为我计划将来使用这个应用程序使用谷歌地图.
谢谢这是我第一次在这里问一个问题.
我刚刚完成了这个.这就是我使用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;
}
}
Run Code Online (Sandbox Code Playgroud)
我省略了对话框方法的代码,因为它们不相关.希望这可以帮助.
| 归档时间: |
|
| 查看次数: |
8955 次 |
| 最近记录: |