解析JSON文件的android代码已经写好了

时间:2014-04-15 02:40:28

标签: java android json

我正在使用此url =“http://nominatim.openstreetmap.org/search?q=26+rue+du+four+75006+paris&format=json&polygon_svg=1&addressdetails=1”(例如)执行请求,并返回一个json文件,我试图解析它:

public class DepartureAddress  {

String adresseDepart = OsmRoadTaskActivity.Estartpoint.getText().toString();
static double lat;
static double lng;

public DepartureAddress() {

}

public static void getGPSCoordonates(String adresseDepart) {
    String adresseDepartFormate = adresseDepart.replaceAll(" ", "+");
    String url = "http://nominatim.openstreetmap.org/search?q="+adresseDepartFormate+"&format=json&polygon_svg=1";
    HttpGet httpget = new HttpGet(url);
    HttpClient client = new DefaultHttpClient();
    HttpResponse response;
    StringBuilder sb = new StringBuilder();
    String result = "";

    try {
        response = client.execute(httpget);
        HttpEntity entity = response.getEntity();
        InputStream stream = entity.getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                stream, "iso-8859-1"), 8);
        String line = null;
        while ((line = reader.readLine()) != null) {

            sb.append(line + "\n");
        }
        stream.close();
        result = sb.toString();
    }

    catch (IOException e) {

        e.printStackTrace();
    }

    try {

        JSONArray jarray = new JSONArray(result);
        for (int i = 0; i < jarray.length(); i++) {

            JSONObject jobject = jarray.getJSONObject(i);
            lat = jobject.getDouble("lat");
            lng = jobject.getDouble("lon");

        }

    } catch (JSONException e) {
        e.printStackTrace();
    }

}

但该应用程序在海洋中间放置了一个标记...... 我错过了什么? 感谢

1 个答案:

答案 0 :(得分:0)

http://nominatim.openstreetmap.org/search?q=26+rue+du+four+75000+paris&format=json&polygon_svg=1&addressdetails=1

返回一个JSON数组而不是一个对象。因此,您需要在JSONArray而不是JSONObject

中获得结果