private double[] pointY =null;
private double[] pointX = null;
ArrayList<HashMap<String, String>> personList;
String myJSON;
private static final String TAG_RESULTS = "result";
private static final String TAG_latitude = "latitude";
private static final String TAG_longitude = "longitude";
private double[] pointY =null;
private double[] pointX = null;
JSONArray peoples = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.listView);
personList = new ArrayList<HashMap<String, String>>();
Button btnMove = (Button) findViewById(R.id.btnMove);
btnMove.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
getData("
http://223.xxx.xxx.xxx/PHP_connection.php"); //수정 필요
}
});
}
protected void showList() {
try {
JSONObject jsonObj = new JSONObject(myJSON);
peoples = jsonObj.getJSONArray(TAG_RESULTS);
int j =0;
for (int i = 0; i < peoples.length(); i++) {
JSONObject c = peoples.getJSONObject(i);
String latitude = c.getString(TAG_latitude);
String longitude = c.getString(TAG_longitude);
HashMap<String, String> persons = new HashMap<String, String>();
persons.put(TAG_latitude,latitude);
persons.put(TAG_longitude,longitude);
personList.add(persons);
// 키로 값을 가져오기
String lat = persons.get(TAG_latitude).toString();
double latitudee = Double.parseDouble(lat);
String lng = persons.get(TAG_latitude).toString();
double longitudee = Double.parseDouble(lng);
pointX[i] = latitudee;
pointY[i] = longitudee;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
public void getData(String url) {
class GetDataJSON extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String uri = params[0];
BufferedReader bufferedReader = null;
try {
URL url = new URL(uri);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
StringBuilder sb = new StringBuilder();
bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String json;
while ((json = bufferedReader.readLine()) != null) {
sb.append(json + "\n");
}
return sb.toString().trim();
} catch (Exception e) {
return null;
}
}
protected void onPostExecute(String result) {
myJSON = result;
showList();
}
}
GetDataJSON g = new GetDataJSON();
g.execute(url);
}
});
대략적인 코드입니다.
제가 하려는건 서버에서 위도 경도 값을 받아와서 각각 배열 pointX, pointY에 순차적으로 저장하고 싶습니다.
궁금한건 HashMap persons에 데이터를 저장하고 키값을 이용하여
String lat = persons.get(TAG_latitude).toString();
double latitudee = Double.parseDouble(lat);
String lng = persons.get(TAG_latitude).toString();
double longitudee = Double.parseDouble(lng);
위도와 경도 값을 각각 변수에 저장합니다. 문제는 배열 pointX, pointY에 순차적으로 데이터를 저장해야 하는데 이게 잘 안되네요 ㅠㅠ
문제는 for문에서의 i가 배열 pointX와 pointY의 범위보다 많거나 적은거 같은데 조언 부탁드립니다 ㅠㅠ
또 궁금한게
ArrayList<HashMap<String, String>> personList; 의 형태인 배열 personList는 어떤식으로 데이터가 저장 되는건가요 ? 조언부탁드립니다...