JsonArray, index search? Or multiple elements in a table

综合技术 2018-01-17 阅读原文

in this example, i like to know which city is closer to Vancouver. Victoria , Washington or Texas? one I find shortest distance how can I see which destination_address this value belong to?

{
   "destination_addresses" : [
      "Victoria, BC, Canada",
      "Texas, États-Unis",
      "Washington, District de Columbia, États-Unis"
   ],
   "origin_addresses" : [ "Vancouver, BC, Canada" ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "135 km",
                  "value" : 134638
               },
               "duration" : {
                  "text" : "6 heures 37 minutes",
                  "value" : 23815
               },
               "status" : "OK"
            },
            {
               "distance" : {
                  "text" : "3 457 km",
                  "value" : 3456694
               },
               "duration" : {
                  "text" : "7 jours 15 heures",
                  "value" : 658855
               },
               "status" : "OK"
            },
            {
               "distance" : {
                  "text" : "5 000 km",
                  "value" : 4999636
               },
               "duration" : {
                  "text" : "11 jours 2 heures",
                  "value" : 956487
               },
               "status" : "OK"
            }
         ]
      }
   ],
   "status" : "OK"
}

I'm trying to find closest city to Vancouver by parsing the distance & find min distance. by finding that we see shortest distance belongs to Victoria. How canI say this min distance belongs to Victoria. this is my code for finding closest city ( shortest distance)

public class MainActivity extends Activity {
HttpClient client;
double minDistance=0;
String destination_addresses;
JSONArray rows;
JSONArray elements;
final static String URL= "http://maps.googleapis.com/maps/api/distancematrix/json?origins=Vancouver+BC&destinations=San+Francisco|Victoria+BC&mode=bicycling&language=fr-FR&sensor=false";
protected void OnCreate (Bundle savedInstanceState ){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    client = new DefaultHttpClient();
    }
public JSONObject ClosestObject (String Origin) throws ClientProtocolException, IOException , JSONException {

    StringBuilder url= new StringBuilder (URL);
    HttpGet get = new HttpGet(url.toString());
    HttpResponse r = client.execute(get);
    int status = r.getStatusLine().getStatusCode();
    if (status == 200){
        HttpEntity e = r.getEntity();
        String data = EntityUtils.toString(e);
        JSONArray locations = new JSONArray(data);
        for (int i=0; i< locations.length(); i++){
            rows=  (JSONArray) locations.getJSONObject(2).get("rows");
            elements= (JSONArray) locations.getJSONObject(0).get("elements");
            String Value= elements.getJSONObject(i).toString();
            double distanceValue = Double.parseDouble(Value);
            if ( distanceValue< minDistance){
                minDistance= distanceValue;
            }
        }

    }
Hello, buddy!

责编内容by:Hello, buddy!阅读原文】。感谢您的支持!

您可能感兴趣的

Android WebView:这是一份全面 & 详细的WebView学习指南... 前言 现在很多 App 里都内置了Web网页( Hybrid App ),比如说很多电商平台,淘宝、京东、聚划算等等,如下图...
Android进阶之旅 – 源码阅读的经验总结... 在长沙待的那些年,身边所看到的大概可分为两类人,一类是不断反复的做业务逻辑,只求功能能够写出来,每天重复上下班,不想过多的去折腾;还有一类是不断的反思总结和学习...
Android—-对于kotlin的一些认识 目录 无标题.png 什么是kotlin? google出的一门android开发语言,号称简洁,编程效率高...
适配Android N (7.0)需要解决的问题汇总... java.lang.SecurityException: MODE_WORLD_READABLE no longer supported异常的原因与解决 M...
话唠,关于重构的一些想法 镇楼,图片来自网络 之前写了一个6000行重构计划,链接在这里 一个6000行MainActivity项目的重构计划 ...