0

我正在尝试使用 Bing Traffic API 获取一些流量数据,但我不断收到 JSONException。

JSON 位于HERE。我使用了一个位于此处的 JSON 格式化程序,以便更好地了解我可以使用的数据。该 url 是公开的,所以我不知道是否需要提供某种身份验证数据,因为我可以将 url 插入浏览器并在此处查看 JSON 结果。

这是我打电话的方式

String url = "http://dev.virtualearth.net/REST/v1/Traffic/Incidents/37,-105,45,-94?key=" + API_KEY;

    // Create JSONParser instance
    JSONParser jParser = new JSONParser();

    // Get JSON from url
    final JSONObject jObject = jParser.getJSONFromUrl(url);


    try {
        JSONArray trafficData = jObject.getJSONObject(TAG_RESOURCESETS).getJSONArray(TAG_RESOURCES);
        Log.w("TrafficIncidentProvider", "Traffic Array consists of " + trafficData.toString());
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

我正在尝试获取名为“resourceSets”的对象和名为“resources”的对象,因为这似乎是我需要的数据嵌入其中的地方。但显然我的做法是错误的,因为 logCat 告诉我认为没有与该数据匹配的值。这是 logcat 给我的错误

  12-10 17:12:42.924: E/JSON Parser(26560): Error parsing data  org.json.JSONException:   End of input at character 0 of 
  12-10 17:12:42.944: W/System.err(26560): org.json.JSONException: No value for  resourceSets
  12-10 17:12:43.004: W/System.err(26560):  at org.json.JSONObject.get(JSONObject.java:354)
  12-10 17:12:43.004: W/System.err(26560):  at org.json.JSONObject.getJSONObject(JSONObject.java:569)
  12-10 17:12:43.014: W/System.err(26560):  at com.brightr.weathermate.providers.TrafficIncidentProvider.getTrafficIncidents(TrafficIncidentProvider.java:43)
  12-10 17:12:43.014: W/System.err(26560):  at com.brightr.weathermate.activities.LocationMapview$showTrafficConditions.doInBackground(LocationMapview.java:323)
  12-10 17:12:43.024: W/System.err(26560):  at com.brightr.weathermate.activities.LocationMapview$showTrafficConditions.doInBackground(LocationMapview.java:1)
  12-10 17:12:43.034: W/System.err(26560):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
  12-10 17:12:43.034: W/System.err(26560):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
  12-10 17:12:43.044: W/System.err(26560):  at java.util.concurrent.FutureTask.run(FutureTask.java:138)
  12-10 17:12:43.044: W/System.err(26560):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
  12-10 17:12:43.044: W/System.err(26560):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)

伙计们,任何关于这个问题的澄清将不胜感激。一如既往的感谢!

编辑:根据要求,这是我的 JSONParser 代码,其中包含我的 HttpPost 方法

 public JSONObject getJSONFromUrl(String url) {

    // Making HTTP request
    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        HttpResponse httpResponse = httpClient.execute(httpPost);


        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();          

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

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parsing the string to a JSON object
    try {
        if(json != null){
        jObj = new JSONObject(json);
        }else{
            jObj = null;
        }
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;

}
4

1 回答 1

1

将此 json 解析为:

         try {
                JSONObject jobject=new JSONObject("Your_json_String");
                JSONArray jarray=jobject.getJSONArray("resourceSets");
                  System.out.println("dateNow jarray :"+jarray.length());
                  for(int i=0;i<jarray.length();i++){
                      if(!jarray.isNull(i)){
                      JSONObject jobjresources=jarray.getJSONObject(i);
                      System.out.println("dateNow jobjresources :"+jobjresources.length());
                      //estimatedTotal
                      if(!jobjresources.isNull("estimatedTotal")){
                        String str_estimatedTotal=jobjresources.getString("estimatedTotal");
                        System.out.println("resources str_estimatedTotal :"+str_estimatedTotal);

                      }
                      else{
                          System.out.println("resources str_estimatedTotal NULL for :"+i+" ITEM");
                      }
                        if(!jobjresources.isNull("resources")){
                        //resources
                        JSONArray jarrresources=jobjresources.getJSONArray("resources");
                        for(int j=0;j<jarrresources.length();j++){
                            System.out.println("$$$$$$$$$$ ITEM "+j+" START $$$$$$$$$$$$$$$$#");
                            if(!jarrresources.isNull(j)){

                            JSONObject jobjjarrresources=jarrresources.getJSONObject(j);
                            if(!jobjjarrresources.isNull("__type")){
                              //__type"
                            String str_type=jobjjarrresources.getString("__type");
                            System.out.println("resources str_type :"+str_type);
                            }
                            else{
                                System.out.println("resources __type NULL for :"+j+" ITEM");
                            }
                              //description"
                            if(!jobjjarrresources.isNull("description")){
                            String strdescription=jobjjarrresources.getString("description");
                            System.out.println("resources description :"+strdescription);
                            }
                            else{
                                System.out.println("resources description NULL for :"+j+" ITEM");
                            }
                              //lane"
                            if(!jobjjarrresources.isNull("lane")){
                            String strlane=jobjjarrresources.getString("lane");
                            System.out.println("resources lane :"+strlane);
                            }
                            else{
                                System.out.println("resources lane NULL for :"+j+" ITEM");
                            }
                              //lane"
                            if(!jobjjarrresources.isNull("point")){
                                JSONObject jobjpoint=jobjjarrresources.getJSONObject("point");
                                //point
                                if(!jobjpoint.isNull("coordinates")){
                                JSONArray jarcoordinates=jobjpoint.getJSONArray("coordinates");
                                for(int k=0;k<jarcoordinates.length();k++){
                                    //JSONObject jobjcoordinates=jarcoordinates.getString(k);
                                     if(!jarcoordinates.isNull(k)){
                                    String str_zero=jarcoordinates.getString(k);
                                    System.out.println("coordinates :"+k+": "+str_zero);
                                     }
                                     else{
                                         System.out.println("coordinates :"+k+" is NULL:"+j+" ITEM");
                                     }
                                 }
                                }
                                else{
                                    System.out.println("resources coordinates NULL for :"+j+" ITEM");
                                }
                            }
                            else{
                                System.out.println("resources point NULL for :"+j+" ITEM");
                            }

                              //roadClosed"
                              //lane"
                            if(!jobjjarrresources.isNull("roadClosed")){

                            String strroadClosed=jobjjarrresources.getString("roadClosed");
                            System.out.println("resources roadClosed :"+strroadClosed);
                            }
                            else{
                                System.out.println("resources roadClosed NULL for :"+j+" ITEM");
                            }
                              //severity"
                            if(!jobjjarrresources.isNull("severity")){
                            String strroadseverity=jobjjarrresources.getString("severity");
                            System.out.println("resources severity :"+strroadseverity);
                            }
                            else
                            {
                                System.out.println("resources severity NULL for :"+j+" ITEM");
                            }
                        }
                            else{
                                System.out.println("jarrresources    NULL for :"+j+" ITEM");
                            }

                            System.out.println("##################### ITEM "+j+" END ##############");
                        }
                       }
                        else{
                            System.out.println("resources    NULL for :"+i+" ITEM");
                        }
                  }

                   else{
                       System.out.println("resources     NULL for : ITEM");
                      }  
          }


            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

}

于 2012-12-10T22:32:04.903 回答