2014-05-09 122 views
0

我正在使用mapquest導航。我正在使用JavaScript代碼來檢索數據。mapquest:如何解析mapquest json請求

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; 

我正在嘗試進行敘述式導航。我該怎麼做?

我在做對吧?

在此先感謝

+0

好像沒有JSONP JSON ... – Selvin

+0

究竟你想知道些什麼?如何將返回的字符串解析爲JSON或如何使用JSON創建敘述性導航?或兩者? – mrunde

回答

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(); 
     } 

     } 

    }