質問編集履歴
4
質問変更
title
CHANGED
File without changes
|
body
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
|
4
4
|
下記のようにサーバーでJson形式のデータを作成します。
|
5
5
|
```
|
6
|
-
locations:[
|
6
|
+
{"locations":[
|
7
7
|
{
|
8
8
|
"latitube":35.680795,
|
9
9
|
"longitube":139.76721
|
@@ -20,20 +20,12 @@
|
|
20
20
|
"latitube":35.671395524875145,
|
21
21
|
"longitube":139.7557784251785
|
22
22
|
}
|
23
|
-
]
|
23
|
+
]}
|
24
|
-
|
25
24
|
```
|
26
25
|
そのデータをAndroid側で受け取って
|
27
|
-
GoogleMapsで表示させたいと考えており
|
26
|
+
GoogleMapsで表示させたいと考えておりす。
|
28
27
|
|
29
|
-
その時の
|
30
|
-
サーバー側での受け渡し方と
|
31
|
-
|
32
|
-
Android側での受け取り方をご教授いただければと思います。
|
33
|
-
|
34
|
-
---
|
35
|
-
【変更】
|
36
|
-
サーバー側での
|
28
|
+
サーバー側でのJSON生成のコードが下記になります。
|
37
29
|
```Java
|
38
30
|
@WebServlet(name = "MakeJsonInfo" , urlPatterns = { "/MakeJsonInfo" })
|
39
31
|
public class MakeJsonInfo extends HttpServlet{
|
@@ -143,12 +135,11 @@
|
|
143
135
|
}
|
144
136
|
}
|
145
137
|
```
|
146
|
-
上記のようになっており、
|
147
|
-
onPostExecuteメソッドの引数のresultがNullPointerExceptionになってしまっており、
|
148
|
-
MainActivityに対して返せていない状況です。
|
149
|
-
|
150
138
|
デバッグを用いると、
|
151
|
-
hGetMethodのuri=nullとなっているので、downloadLocationメソッド
|
139
|
+
hGetMethodのuri=nullとなっているので、downloadLocationメソッド
|
140
|
+
の
|
152
|
-
|
141
|
+
HttpResponse resp = hClient.execute(hGetMethod);
|
142
|
+
が問題なのかと思います。
|
143
|
+
この場合、HTTP通信ができていないということなのでしょうか?
|
153
144
|
|
154
|
-
よろしくお願いいたします。
|
145
|
+
ご教授のほどよろしくお願いいたします。
|
3
質問編集
title
CHANGED
File without changes
|
body
CHANGED
@@ -85,4 +85,70 @@
|
|
85
85
|
}
|
86
86
|
```
|
87
87
|
|
88
|
+
Android側
|
89
|
+
MainActivityになります。
|
90
|
+
```Java
|
91
|
+
MainActivity
|
92
|
+
|
93
|
+
AsyncDownload asyncTask = new AsyncDownload(mapsActivity);
|
94
|
+
asyncTask.execute("http://localhost:8080/プロジェクト名/アノテーションのurlPatterns?lat=" + point.latitude + "&lon=" + point.longitude + "&num_of_res=" + NumOfRes);
|
95
|
+
```
|
96
|
+
AsyncDownloadクラスです。
|
97
|
+
```Java
|
98
|
+
@SuppressWarnings("deprecation")
|
99
|
+
public class AsyncDownload extends AsyncTask<String, Integer, String> {
|
100
|
+
|
101
|
+
private HttpClient hClient;
|
102
|
+
private HttpGet hGetMethod;
|
103
|
+
private MapsActivity mainActivity;
|
104
|
+
private OnCameraChangeListener onCameraChangeListener;
|
105
|
+
// コンストラクター
|
106
|
+
public AsyncDownload(MapsActivity mainActivity2){
|
107
|
+
mainActivity = mainActivity2;
|
108
|
+
hClient = new DefaultHttpClient();
|
109
|
+
hGetMethod = new HttpGet();
|
110
|
+
}
|
111
|
+
|
112
|
+
// バックグラウンドで処理
|
113
|
+
@Override
|
114
|
+
protected String doInBackground(String... params) {
|
115
|
+
String uri = params[0];
|
116
|
+
return downloadLocations(uri);
|
117
|
+
}
|
118
|
+
|
119
|
+
// バックグラウンド処理が終了した後にメインスレッドに渡す処理
|
120
|
+
protected void onPostExecute(String result) {
|
121
|
+
try{
|
122
|
+
mainActivity.setResultLocations(result);
|
123
|
+
}catch(NullPointerException e){
|
124
|
+
}
|
125
|
+
}
|
126
|
+
|
127
|
+
// 位置情報(JSON)をダウンロード
|
128
|
+
private String downloadLocations(String uri) {
|
129
|
+
try {
|
130
|
+
hGetMethod.setURI(new URI(uri));
|
131
|
+
HttpResponse resp = hClient.execute(hGetMethod);
|
132
|
+
Log.d("DownloadLocations","status="+resp.getStatusLine().getStatusCode());
|
133
|
+
if (resp.getStatusLine().getStatusCode() < 400) {
|
134
|
+
String str = EntityUtils.toString(resp.getEntity(), HTTP.UTF_8);
|
135
|
+
Log.d("DownloadLocations","res="+str);
|
136
|
+
return str;
|
137
|
+
}
|
138
|
+
} catch (Exception e) {
|
139
|
+
Log.d("DownloadLocations","error");
|
140
|
+
e.printStackTrace();
|
141
|
+
}
|
142
|
+
return null;
|
143
|
+
}
|
144
|
+
}
|
145
|
+
```
|
146
|
+
上記のようになっており、
|
147
|
+
onPostExecuteメソッドの引数のresultがNullPointerExceptionになってしまっており、
|
148
|
+
MainActivityに対して返せていない状況です。
|
149
|
+
|
150
|
+
デバッグを用いると、
|
151
|
+
hGetMethodのuri=nullとなっているので、downloadLocationメソッドが問題なのかと思っております。
|
152
|
+
また、doInBackgroundメソッドのuriにはMainActivityのuriが入ってきております。
|
153
|
+
|
88
154
|
よろしくお願いいたします。
|
2
変更
title
CHANGED
File without changes
|
body
CHANGED
@@ -32,15 +32,21 @@
|
|
32
32
|
Android側での受け取り方をご教授いただければと思います。
|
33
33
|
|
34
34
|
---
|
35
|
-
【
|
35
|
+
【変更】
|
36
36
|
サーバー側でのJson生成処理になります。
|
37
37
|
```Java
|
38
|
+
@WebServlet(name = "MakeJsonInfo" , urlPatterns = { "/MakeJsonInfo" })
|
38
|
-
public class MakeJsonInfo{
|
39
|
+
public class MakeJsonInfo extends HttpServlet{
|
39
|
-
|
40
|
+
private static final long serialVersionUID = 1L;
|
41
|
+
|
42
|
+
@Override
|
43
|
+
protected void doGet(HttpServletRequest request, HttpServletResponse response)
|
44
|
+
throws ServletException, IOException {
|
40
45
|
DbSelect dbselect = new DbSelect();
|
41
46
|
LinkedList<Locations> locationsList = new LinkedList<>();
|
42
47
|
|
43
48
|
Gson gson = new Gson();
|
49
|
+
PrintWriter out = response.getWriter();
|
44
50
|
try{
|
45
51
|
dbselect.connect();
|
46
52
|
|
@@ -54,13 +60,19 @@
|
|
54
60
|
Locations locations = new Locations(lat , lon);
|
55
61
|
locationsList.add(locations);
|
56
62
|
}
|
57
|
-
String jsonFile = "locations:" + gson.toJson(locationsList);
|
63
|
+
String jsonFile = "{\"locations\":\"" + gson.toJson(locationsList) + "\"}";
|
58
|
-
|
64
|
+
out.println(jsonFile);
|
59
65
|
}catch(Exception e){
|
60
66
|
e.printStackTrace();
|
61
67
|
}
|
62
68
|
}
|
63
69
|
|
70
|
+
@Override
|
71
|
+
protected void doPost(HttpServletRequest request, HttpServletResponse response)
|
72
|
+
throws ServletException, IOException {
|
73
|
+
doGet(request, response);
|
74
|
+
}
|
75
|
+
|
64
76
|
static class Locations{
|
65
77
|
private double lat;
|
66
78
|
private double lon;
|
1
質問変更
title
CHANGED
File without changes
|
body
CHANGED
@@ -31,4 +31,46 @@
|
|
31
31
|
|
32
32
|
Android側での受け取り方をご教授いただければと思います。
|
33
33
|
|
34
|
+
---
|
35
|
+
【追記】
|
36
|
+
サーバー側でのJson生成処理になります。
|
37
|
+
```Java
|
38
|
+
public class MakeJsonInfo{
|
39
|
+
public static void main(String[] args){
|
40
|
+
DbSelect dbselect = new DbSelect();
|
41
|
+
LinkedList<Locations> locationsList = new LinkedList<>();
|
42
|
+
|
43
|
+
Gson gson = new Gson();
|
44
|
+
try{
|
45
|
+
dbselect.connect();
|
46
|
+
|
47
|
+
double lat = 0;
|
48
|
+
double lon = 0;
|
49
|
+
int listCount = dbselect.detailListAllCount(1);
|
50
|
+
for(int i = 0; i < listCount; i++){
|
51
|
+
lat = dbselect.selectInfoList().get(i).getLatitube();
|
52
|
+
lon = dbselect.selectInfoList().get(i).getLongitube();
|
53
|
+
|
54
|
+
Locations locations = new Locations(lat , lon);
|
55
|
+
locationsList.add(locations);
|
56
|
+
}
|
57
|
+
String jsonFile = "locations:" + gson.toJson(locationsList);
|
58
|
+
System.out.println(jsonFile);
|
59
|
+
}catch(Exception e){
|
60
|
+
e.printStackTrace();
|
61
|
+
}
|
62
|
+
}
|
63
|
+
|
64
|
+
static class Locations{
|
65
|
+
private double lat;
|
66
|
+
private double lon;
|
67
|
+
|
68
|
+
public Locations(double latitube , double longitube){
|
69
|
+
this.lat = latitube;
|
70
|
+
this.lon = longitube;
|
71
|
+
}
|
72
|
+
}
|
73
|
+
}
|
74
|
+
```
|
75
|
+
|
34
76
|
よろしくお願いいたします。
|