質問編集履歴

4

質問変更

2015/09/29 05:31

投稿

GH_usami13
GH_usami13

スコア24

test CHANGED
File without changes
test CHANGED
@@ -8,7 +8,7 @@
8
8
 
9
9
  ```
10
10
 
11
- locations:[
11
+ {"locations":[
12
12
 
13
13
  {
14
14
 
@@ -42,33 +42,17 @@
42
42
 
43
43
  }
44
44
 
45
- ]
45
+ ]}
46
-
47
-
48
46
 
49
47
  ```
50
48
 
51
49
  そのデータをAndroid側で受け取って
52
50
 
53
- GoogleMapsで表示させたいと考えておりす。
51
+ GoogleMapsで表示させたいと考えておりす。
54
-
55
-
56
-
57
- その時の
52
+
58
-
59
- サーバー側での受け渡し方と
53
+
60
-
61
-
62
-
63
- Android側での受け取り方をご教授いただければと思います。
54
+
64
-
65
-
66
-
67
- ---
68
-
69
- 【変更】
70
-
71
- サーバー側でのJson生成処理になります。
55
+ サーバー側でのJSON生成のコードが下記になります。
72
56
 
73
57
  ```Java
74
58
 
@@ -288,20 +272,18 @@
288
272
 
289
273
  ```
290
274
 
291
- 上記のようになっており、
292
-
293
- onPostExecuteメソッドの引数のresultがNullPointerExceptionになってしまっており、
294
-
295
- MainActivityに対して返せていない状況です。
296
-
297
-
298
-
299
275
  デバッグを用いると、
300
276
 
301
- hGetMethodのuri=nullとなっているので、downloadLocationメソッドが問題なのかと思っております。
277
+ hGetMethodのuri=nullとなっているので、downloadLocationメソッド
278
+
302
-
279
+
280
+
303
- また、doInBackgroundメソッドのuriにはMainActivityのuriが入ってきております。
281
+ HttpResponse resp = hClient.execute(hGetMethod);
282
+
304
-
283
+ が問題なのかと思います。
284
+
305
-
285
+ この場合、HTTP通信ができていないということなのでしょうか?
306
-
286
+
287
+
288
+
307
- よろしくお願いいたします。
289
+ ご教授のほどよろしくお願いいたします。

3

質問編集

2015/09/29 05:31

投稿

GH_usami13
GH_usami13

スコア24

test CHANGED
File without changes
test CHANGED
@@ -172,4 +172,136 @@
172
172
 
173
173
 
174
174
 
175
+ Android側
176
+
177
+ MainActivityになります。
178
+
179
+ ```Java
180
+
181
+ MainActivity
182
+
183
+
184
+
185
+ AsyncDownload asyncTask = new AsyncDownload(mapsActivity);
186
+
187
+ asyncTask.execute("http://localhost:8080/プロジェクト名/アノテーションのurlPatterns?lat=" + point.latitude + "&lon=" + point.longitude + "&num_of_res=" + NumOfRes);
188
+
189
+ ```
190
+
191
+ AsyncDownloadクラスです。
192
+
193
+ ```Java
194
+
195
+ @SuppressWarnings("deprecation")
196
+
197
+ public class AsyncDownload extends AsyncTask<String, Integer, String> {
198
+
199
+
200
+
201
+ private HttpClient hClient;
202
+
203
+ private HttpGet hGetMethod;
204
+
205
+ private MapsActivity mainActivity;
206
+
207
+ private OnCameraChangeListener onCameraChangeListener;
208
+
209
+ // コンストラクター
210
+
211
+ public AsyncDownload(MapsActivity mainActivity2){
212
+
213
+ mainActivity = mainActivity2;
214
+
215
+ hClient = new DefaultHttpClient();
216
+
217
+ hGetMethod = new HttpGet();
218
+
219
+ }
220
+
221
+
222
+
223
+ // バックグラウンドで処理
224
+
225
+ @Override
226
+
227
+ protected String doInBackground(String... params) {
228
+
229
+ String uri = params[0];
230
+
231
+ return downloadLocations(uri);
232
+
233
+ }
234
+
235
+
236
+
237
+ // バックグラウンド処理が終了した後にメインスレッドに渡す処理
238
+
239
+ protected void onPostExecute(String result) {
240
+
241
+ try{
242
+
243
+ mainActivity.setResultLocations(result);
244
+
245
+ }catch(NullPointerException e){
246
+
247
+ }
248
+
249
+ }
250
+
251
+
252
+
253
+ // 位置情報(JSON)をダウンロード
254
+
255
+ private String downloadLocations(String uri) {
256
+
257
+ try {
258
+
259
+ hGetMethod.setURI(new URI(uri));
260
+
261
+ HttpResponse resp = hClient.execute(hGetMethod);
262
+
263
+ Log.d("DownloadLocations","status="+resp.getStatusLine().getStatusCode());
264
+
265
+ if (resp.getStatusLine().getStatusCode() < 400) {
266
+
267
+ String str = EntityUtils.toString(resp.getEntity(), HTTP.UTF_8);
268
+
269
+ Log.d("DownloadLocations","res="+str);
270
+
271
+ return str;
272
+
273
+ }
274
+
275
+ } catch (Exception e) {
276
+
277
+ Log.d("DownloadLocations","error");
278
+
279
+ e.printStackTrace();
280
+
281
+ }
282
+
283
+ return null;
284
+
285
+ }
286
+
287
+ }
288
+
289
+ ```
290
+
291
+ 上記のようになっており、
292
+
293
+ onPostExecuteメソッドの引数のresultがNullPointerExceptionになってしまっており、
294
+
295
+ MainActivityに対して返せていない状況です。
296
+
297
+
298
+
299
+ デバッグを用いると、
300
+
301
+ hGetMethodのuri=nullとなっているので、downloadLocationメソッドが問題なのかと思っております。
302
+
303
+ また、doInBackgroundメソッドのuriにはMainActivityのuriが入ってきております。
304
+
305
+
306
+
175
307
  よろしくお願いいたします。

2

変更

2015/09/29 00:33

投稿

GH_usami13
GH_usami13

スコア24

test CHANGED
File without changes
test CHANGED
@@ -66,15 +66,25 @@
66
66
 
67
67
  ---
68
68
 
69
- 追記
69
+ 変更
70
70
 
71
71
  サーバー側でのJson生成処理になります。
72
72
 
73
73
  ```Java
74
74
 
75
- public class MakeJsonInfo{
75
+ @WebServlet(name = "MakeJsonInfo" , urlPatterns = { "/MakeJsonInfo" })
76
76
 
77
+ public class MakeJsonInfo extends HttpServlet{
78
+
77
- public static void main(String[] args){
79
+ private static final long serialVersionUID = 1L;
80
+
81
+
82
+
83
+ @Override
84
+
85
+ protected void doGet(HttpServletRequest request, HttpServletResponse response)
86
+
87
+ throws ServletException, IOException {
78
88
 
79
89
  DbSelect dbselect = new DbSelect();
80
90
 
@@ -83,6 +93,8 @@
83
93
 
84
94
 
85
95
  Gson gson = new Gson();
96
+
97
+ PrintWriter out = response.getWriter();
86
98
 
87
99
  try{
88
100
 
@@ -110,15 +122,27 @@
110
122
 
111
123
  }
112
124
 
113
- String jsonFile = "locations:" + gson.toJson(locationsList);
125
+ String jsonFile = "{\"locations\":\"" + gson.toJson(locationsList) + "\"}";
114
126
 
115
- System.out.println(jsonFile);
127
+ out.println(jsonFile);
116
128
 
117
129
  }catch(Exception e){
118
130
 
119
131
  e.printStackTrace();
120
132
 
121
133
  }
134
+
135
+ }
136
+
137
+
138
+
139
+ @Override
140
+
141
+ protected void doPost(HttpServletRequest request, HttpServletResponse response)
142
+
143
+ throws ServletException, IOException {
144
+
145
+ doGet(request, response);
122
146
 
123
147
  }
124
148
 

1

質問変更

2015/09/28 13:56

投稿

GH_usami13
GH_usami13

スコア24

test CHANGED
File without changes
test CHANGED
@@ -64,4 +64,88 @@
64
64
 
65
65
 
66
66
 
67
+ ---
68
+
69
+ 【追記】
70
+
71
+ サーバー側でのJson生成処理になります。
72
+
73
+ ```Java
74
+
75
+ public class MakeJsonInfo{
76
+
77
+ public static void main(String[] args){
78
+
79
+ DbSelect dbselect = new DbSelect();
80
+
81
+ LinkedList<Locations> locationsList = new LinkedList<>();
82
+
83
+
84
+
85
+ Gson gson = new Gson();
86
+
87
+ try{
88
+
89
+ dbselect.connect();
90
+
91
+
92
+
93
+ double lat = 0;
94
+
95
+ double lon = 0;
96
+
97
+ int listCount = dbselect.detailListAllCount(1);
98
+
99
+ for(int i = 0; i < listCount; i++){
100
+
101
+ lat = dbselect.selectInfoList().get(i).getLatitube();
102
+
103
+ lon = dbselect.selectInfoList().get(i).getLongitube();
104
+
105
+
106
+
107
+ Locations locations = new Locations(lat , lon);
108
+
109
+ locationsList.add(locations);
110
+
111
+ }
112
+
113
+ String jsonFile = "locations:" + gson.toJson(locationsList);
114
+
115
+ System.out.println(jsonFile);
116
+
117
+ }catch(Exception e){
118
+
119
+ e.printStackTrace();
120
+
121
+ }
122
+
123
+ }
124
+
125
+
126
+
127
+ static class Locations{
128
+
129
+ private double lat;
130
+
131
+ private double lon;
132
+
133
+
134
+
135
+ public Locations(double latitube , double longitube){
136
+
137
+ this.lat = latitube;
138
+
139
+ this.lon = longitube;
140
+
141
+ }
142
+
143
+ }
144
+
145
+ }
146
+
147
+ ```
148
+
149
+
150
+
67
151
  よろしくお願いいたします。