mapquest:如何解析mapquest json请求

时间:2014-05-09 10:32:16

标签: javascript android json jsonp mapquest

我正在使用mapquest导航。我正在使用javascript代码来检索数据。

http://www.mapquestapi.com/directions/v2/route?key=YOUR_KEY_HERE&from=Lancaster,PA&to=York,PA&callback=renderNarrative

我在应用程序中获取json内容但不知道如何使用该数据进行导航。我创建了一个新项目并运行代码

class jsonapp extends AsyncTask<String, Void, String>{

@Override
protected String doInBackground(String... uri) {
    // TODO Auto-generated method stub
    HttpClient httpclient = new DefaultHttpClient();
    HttpResponse response;
    String responseString = null;
    try {
        response = httpclient.execute(new HttpGet(uri[0]));
        StatusLine statusLine = response.getStatusLine();
        if(statusLine.getStatusCode() == HttpStatus.SC_OK){
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            response.getEntity().writeTo(out);
            out.close();
            responseString = out.toString();

            Log.d("Filter", ""+responseString);
        } else{
            //Closes the connection.
            response.getEntity().getContent().close();
            throw new IOException(statusLine.getReasonPhrase());
        }
    } catch (ClientProtocolException e) {
        //TODO Handle problems..
    } catch (IOException e) {
        //TODO Handle problems..
    }
    return responseString;

我正在尝试进行叙事导航。我该怎么办?

我正在做对吗?

提前致谢

1 个答案:

答案 0 :(得分:0)

这件事对我有用:

我已经为路线矩阵完成了相同的路线尝试。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URLEncoder;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;

    public class ApacheHttpClientGet {

        static DefaultHttpClient httpClient = new DefaultHttpClient();
        static HttpGet getRequest;
        static HttpResponse response;

        public static void main(String[] args) {
            try {

         getRequest = new HttpGet("http://www.mapquestapi.com/directions/v2/routematrix?key=XFZxlqQQJbNaABkGXuaRfUHSIdYXuIBh&.format(lat,long)&inFormat=kvp&outFormat=json&from=BuddhdaveColony,vadodara,gujarat&to=oldpadraroad,vadodara,gujarat&unit=k");

        response = httpClient.execute(getRequest);


        if (response.getStatusLine().getStatusCode() != 200) {
                    throw new RuntimeException("Failed : HTTP error code : "
                            + response.getStatusLine().getStatusCode());

        }


        BufferedReader br = new BufferedReader(new InputStreamReader(
                        (response.getEntity().getContent())));

        String output;
        String jsonData="";
        System.out.println("Output from Server .... \n");
        while ((output = br.readLine()) != null) {

                    jsonData += output + "\n";
                    System.out.println(jsonData);

        }

        JSONObject obj = new JSONObject(jsonData);
        System.out.println("allToAll: " + obj.getBoolean("allToAll"));
        System.out.println("distance: " + obj.getJSONArray("distance"));

        httpClient.getConnectionManager().shutdown();

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
                e.printStackTrace();
        }

        }

    }