아래에서 볼 수있는 것처럼 코드는 Google API에 연결하여 도시 간 거리를 계산합니다. AsyncTask를 "for"루프 안에 넣습니다. 마지막으로 배열 끝에 "diff"값을 넣으려고합니다. 제발 코드의 목적을 게을리하지 마세요, 그건 내가 묻고있는 건 중요하지 않습니다. 내 문제 : 첫 번째 "for"루프의 "w"int는 "b"로 모든 값을 취합니다 전에 DownloadTask 클래스가 실행 중일 것 같습니다. 그 때문에, "itinList1.get (w) .put ("diff ", String.valueOf (0));" 끝 부분 라인 값 B 한번에 소요되지 0,1 값 ... (B) 예상 나 로그 캣에 IndexOutOfBoundsException가 "메시지"m은 점점 "한다. 미리 감사."For"루프는 AsyncTask가 실행되기 전에 모든 값을 사용합니다.
for (w = 0; w < b; w++) {
markerPoints = new ArrayList<LatLng>();
Log.d("w1", String.valueOf(w));
if(itinList1.get(w).get("username").equals(username)){
origin_lat = itinnList1.get(w).get("start_lat_pro").toString();
origin_lng = itinList1.get(w).get("start_lng_pro").toString();
destination_lat = itinList1.get(w).get("final_lat_pro").toString();
destination_lng = itinList1.get(w).get("final_lng_pro").toString();
for(w1=w+1;w1<b;w1++){
if(!username.equals(itinList1.get(w1).get("username"))){
waypoint1_lat = itinList1.get(w1).get("start_lat_pro").toString();
waypoint1_lng = itinList1.get(w1).get("start_lng_pro").toString();
waypoint2_lat = itinList1.get(w1).get("final_lat_pro").toString();
waypoint2_lng = itinList1.get(w1).get("final_lng_pro").toString();
LatLng origin1 = new LatLng(Double.parseDouble(origin_lat),
Double.parseDouble(origin_lng));
LatLng destination = new LatLng(
Double.parseDouble(destination_lat),
Double.parseDouble(destination_lng));
LatLng waypoint1 = new LatLng(
Double.parseDouble(waypoint1_lat),
Double.parseDouble(waypoint1_lng));
LatLng waypoint2 = new LatLng(
Double.parseDouble(waypoint2_lat),
Double.parseDouble(waypoint2_lng));
markerPoints.add(origin1);
markerPoints.add(destination);
markerPoints.add(waypoint1);
markerPoints.add(waypoint2);
LatLng or = markerPoints.get(0);
LatLng dest = markerPoints.get(1);
// Getting URL to the Google Directions API
String url = getDirectionsUrl(or, dest);
DownloadTask downloadTask = new DownloadTask();
// Start downloading json data from Google Directions API
downloadTask.execute(url);
} else {
break;
}
}
}
}
}
private String getDirectionsUrl(LatLng or, LatLng dest) {
// Origin of route
String str_origin = "origin=" + or.latitude + "," + or.longitude;
// Destination of route
String str_dest = "destination=" + dest.latitude + ","
+ dest.longitude;
// Sensor enabled
String sensor = "sensor=false";
// Waypoints
LatLng way1 = markerPoints.get(2);
LatLng way2 = markerPoints.get(3);
// Waypoints
waypoints = "";
for (int i = 2; i < markerPoints.size(); i++) {
LatLng point = (LatLng) markerPoints.get(i);
if (i == 2)
waypoints = "waypoints=";
waypoints += point.latitude + "," + point.longitude + "|";
}
// Building the parameters to the web service
String parameters = str_origin + "&" + str_dest + "&" + sensor
+ "&" + waypoints;
// Output format
String output = "json";
// Building the url to the web service
String url = "https://maps.googleapis.com/maps/api/directions/"
+ output + "?" + parameters;
return url;
}
private String downloadUrl(String strUrl) throws IOException {
String data = "";
InputStream iStream = null;
HttpURLConnection urlConnection = null;
try {
URL url = new URL(strUrl);
// Creating an http connection to communicate with url
urlConnection = (HttpURLConnection) url.openConnection();
// Connecting to url
urlConnection.connect();
// Reading data from url
iStream = urlConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(
iStream));
StringBuffer sb = new StringBuffer();
String line = "";
while ((line = br.readLine()) != null) {
sb.append(line);
}
data = sb.toString();
br.close();
} catch (Exception e) {
Log.d("Exception while downloading url", e.toString());
} finally {
iStream.close();
urlConnection.disconnect();
}
return data;
}
private class DownloadTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... url) {
// For storing data from web service
String data = "";
try {
// Fetching the data from web service
data = downloadUrl(url[0]);
} catch (Exception e) {
Log.d("Background Task", e.toString());
}
return data;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
ParserTask parserTask = new ParserTask();
// Invokes the thread for parsing the JSON data
parserTask.execute(result);
}
}
public class ParserTask extends
AsyncTask<String, Integer, ArrayList<HashMap<String, String>>> {
@Override
public void onPreExecute() {
super.onPreExecute();
}
@Override
protected ArrayList<HashMap<String, String>> doInBackground(
String... jsonData) {
JSONObject jObject;
List<List<HashMap<String, String>>> routes = null;
try {
jObject = new JSONObject(jsonData[0]);
DirectionsJSONParser1 parser = new DirectionsJSONParser1();
// Starts parsing data
routes = parser.parse(jObject);
JSONArray routeObject = jObject.getJSONArray("routes");
JSONObject first = routeObject.getJSONObject(0);
JSONArray legs = first.getJSONArray("legs");
z = 0;
for (i = 0; i < legs.length(); i++) {
JSONObject legs1 = legs.getJSONObject(i);
JSONObject distanceObject = legs1.getJSONObject("distance");
String distance = distanceObject.getString("text");
String asd[] = distance.split(" ");
double x = Double.parseDouble(asd[0]);
if (asd[1].equals("m")) {
x = x/1000;
}
x = Math.round(x);
z = z + x;
}
sum.add(z);
if (sum.size() < 2) {
q = 0;
**//here is the problem..**
itinList1.get(w).put("diff", String.valueOf(0));
} else {
q = z - sum.get(w);
if (q < 0) {
q = 0;
}
itinList1.get(sum.size() - 1).put("diff",
String.valueOf(q));
}
} catch (Exception e) {
e.printStackTrace();
}
Log.d("q", String.valueOf(q));
return itinList1;
}
// Executes in UI thread, after the parsing process
@Override
protected void onPostExecute(
ArrayList<HashMap<String, String>> result) {
....rest of code....
를 내가 며칠 전에 비슷한 주제를 게시 . 이것은 나에게 다른 사람들 사이에서 가장 확실한 설명 인 것처럼 보인다. 고마워 Greg –
그럴 경우 다른 사람들이 알 수 있도록 정답으로 표시해주십시오. –