Repository navigation
Expand file tree
/
Copy pathdetail_book.java
More file actions
80 lines (71 loc) · 2.85 KB
/
Copy pathdetail_book.java
File metadata and controls
80 lines (71 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
///////ここから本検索部分//////////////////////////////////////////////////////////////
String detail(String word){//検索したい本を入力することで,その本のあらすじを返す
String detail_text="";
String text="";
String data = "";
String scheme = "https";
String authority = "www.googleapis.com";
String path = "books/v1/volumes";
String query = word;//検索したい本の名前
Uri.Builder uriBuilder = new Uri.Builder();
uriBuilder.scheme(scheme);
uriBuilder.authority(authority);
uriBuilder.path(path);
uriBuilder.appendQueryParameter("q", query);
String uri = uriBuilder.toString();
HttpClient httpClient = new DefaultHttpClient();
HttpParams params = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, 3000);
HttpConnectionParams.setSoTimeout(params, 3000);
HttpUriRequest httpRequest = new HttpGet(uri);
HttpResponse httpResponse = null;
try {
httpResponse = httpClient.execute(httpRequest);
}
catch (ClientProtocolException e) {
//例外処理
}
catch (IOException e){
//例外処理
}
if (httpResponse != null && httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
HttpEntity httpEntity = httpResponse.getEntity();
try {
data = EntityUtils.toString(httpEntity);
}
catch (ParseException e) {
//例外処理
}
catch (IOException e) {
//例外処理
}
finally {
try {
httpEntity.consumeContent();
}
catch (IOException e) {
//例外処理
}
}
}
//JSON処理///////
try {
//System.out.println("1") ;
JSONObject rootObject = new JSONObject(data);
// System.out.println("2") ;
JSONArray jsonObject = rootObject.getJSONArray("items");
// System.out.println("3") ;
JSONObject jsonObject2 = jsonObject.getJSONObject(0);
// System.out.println("4") ;
JSONObject jsonObject3 = jsonObject2.getJSONObject("volumeInfo");
// System.out.println("5") ;
detail_text =jsonObject3.getString("description");//概要がdetail_textにはいる
// System.out.println("6") ;
}
catch (JSONException e){
return detail_text="該当情報なし";
}
httpClient.getConnectionManager().shutdown();
return detail_text;
}
//////検索部分ここまで/////////////////////////////////////////////////////