-1

您好我正在尝试从foursquare api解析json的输出,但我不断收到错误这是我的代码

public FsqPlace getPlaceDetails(String reference) throws Exception{
    FsqPlace fsqplace= new FsqPlace();

    try {
        HttpRequestFactory httpRequestFactory = createRequestFactory(HTTP_TRANSPORT);
        url = (API_URL+ "venues/"+reference+"?client_id="+CLIENT_ID+"&client_secret="+CLIENT_SECRET); 
        Log.d(Tag, url);


        HttpRequest request = httpRequestFactory
                .buildGetRequest(new GenericUrl(url));
        String response     = streamToString(request.execute().getContent());
        JSONObject json = (JSONObject) new JSONTokener(response).nextValue();

        fsqplace.name = json.getString("name");
        fsqplace.id   = json.getString("id");

        // fsqplace = request.execute().parseAs(FsqVenueDetails.class); 

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        System.out.print(e);

    } 

    return fsqplace;    
}  





  private String streamToString(InputStream is) throws IOException {
    String str  = "";

    if (is != null) {
        StringBuilder sb = new StringBuilder();
        String line;

        try {
            BufferedReader reader   = new BufferedReader(new InputStreamReader(is));

            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }

            reader.close();
        } finally {
            is.close();
        }

        str = sb.toString();
    }

    return str;

}



 public class FsqPlace implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = -1776219712112491139L;

@Key
public String id; 

@Key
public String name; 

@Key
public String address; 

@Key
public String formattedphone;

@Key
public Location location;


@Key
public int ratings;

/*@Key
public Category[] categories;*/

@Override
public String toString() { 
    return name + " - " + id + " - " ; 
} 



public static class Location implements Serializable 
{ 
    @Key
    public double lat; 

    @Key
    public double lng; 
} }

错误信息如下:

11-09 09:22:11.288: W/System.err(31379): org.json.JSONException: No value for name
11-09 09:22:11.288: W/System.err(31379):    at org.json.JSONObject.get(JSONObject.java:354)
11-09 09:22:11.288: W/System.err(31379):    at org.json.JSONObject.getString  (JSONObject.java:510)
11-09 09:22:11.288: W/System.err(31379):    at com.example.reddot.Foursquare.getPlaceDetails(Foursquare.java:159)
11-09 09:22:11.288: W/System.err(31379):    at com.example.reddot.SinglePlaceActivity$LoadSinglePlaceDetails.doInBackground(SinglePlaceActivity.java:212)
11-09 09:22:11.288: W/System.err(31379):    at com.example.reddot.SinglePlaceActivity$LoadSinglePlaceDetails.doInBackground(SinglePlaceActivity.java:1)
11-09 09:22:11.296: W/System.err(31379):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
11-09 09:22:11.296: W/System.err(31379):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
11-09 09:22:11.296: W/System.err(31379):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
11-09 09:22:11.296: W/System.err(31379):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
11-09 09:22:11.296: W/System.err(31379):    at   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
11-09 09:22:11.296: W/System.err(31379):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)

json响应

11-09 10:08:42.749: I/System.out(32470): com.google.api.client.http.HttpRequest@41b2e3f04b2f2e72f964a520ffe924e3{"meta":{"code":200,"errorType":"deprecated","errorDetail":"Please provide an API version to avoid future errors.See http:\/\/bit.ly\/vywCav"},
"response":
   {"venue":
    {"id":"4b2f2e72f964a520ffe924e3","name":"Limkokwing University of Creative    Technology",
  "contact":{"phone":"+60383178888","formattedPhone":"+60 3-8317   8888","twitter":"limkokwing_my"},
"location":{"address":"Inovasi 1-1","crossStreet":"Jalan Teknokrat 1 \/1","lat":2.940105082370034,"lng":101.6625452041626,"postalCode":"63000","city":"Cyberjaya","state":"Selangor","country":"Malaysia","cc":"MY"},
 "categories":  [{"id":"4bf58dd8d48988d1ae941735","name":"University","pluralName":"Universities","shortName":"University","icon":"https:\/\/foursquare.com\/img\/categories\/education\/default.png","parents":["Colleges & Universities"],"primary":true},{"id":"4bf58dd8d48988d19f941735","name":"College Technology Building","pluralName":"College Technology Buildings",
 "shortName":"Technology","icon":"https:\/\/foursquare.com\/img\/categories\/education\/default.png","parents":["Colleges & Universities","College Academic Buildings"]},{"id":"4bf58dd8d48988d1b1941735","name":"College  Bookstore","pluralName":"College Bookstores","shortName":"Bookstore","icon":"https:\/\/foursquare.com\/img\/categories\/education\/bookstore.png","parents":["Colleges & Universities"]}],
"verified":true,"restricted":true,"stats":{"checkinsCount":17188,"usersCount":2924,"tipCount":57},"url":"http:\/\/www.limkokwing.net\/malaysia","likes":{"count":0,"groups":[]},"specials":[],"photos":{"count":231,"groups":[{"type":"checkin","name":"Friends' check-in photos","count":0,"items":[]},{"type":"venue","name":"Venue photos","count":231,"items":[{"id":"509a0288e4b0606e3a97ab27","createdAt":1352270472,"source":{"name":"foursquare for BlackBerry","url":"https:\/\/foursquare.com\/download\/#\/blackberry"},"url":"https:\/\/is0.4sqi.net\/pix\/3324926_gRzWjsYNxPtZoXXvAsGc19rbo-WhEEAXjF4pjtKXIX4.jpg","sizes":{"count":4,"items":[{"url":"https:\/\/is0.4sqi.net\/pix\/3324926_gRzWjsYNxPtZoXXvAsGc19rbo-WhEEAXjF4pjtKXIX4.jpg","width":640,"height":480},{"url":"https:\/\/is1.4sqi.net\/derived_pix\/3324926_gRzWjsYNxPtZoXXvAsGc19rbo-WhEEAXjF4pjtKXIX4_300x300.jpg","width":300,"height":300},{"url":"https:\/\/is1.4sqi.net\/derived_pix\/3324926_gRzWjsYNxPtZoXXvAsGc19rbo-WhEEAXjF4pjtKXIX4_100x100.jpg","width":100,"height":100},{"url":"https:\/\/is1.4sqi.net\/derived_pix\/3324926_gRzWjsYNxPtZoXXvAsGc19rbo-WhEEAXjF4pjtKXIX4_36x36.jpg","width":36,"height":36}]},"user":{"id":"3324926","firstName":"ashaasa","photo":"https:\/\/is0.4sqi.net\/userpix_thumbs\/D5P5X4K24M01KXJ2.jpg","tips":{"count":1},"gender":"female","homeCity":"Jakarta, Indonesia","bio":":)","contact":{}},"visibility":"public"},{"id":"5099031be4b0c377d6b98f1f","createdAt":1352205083,"source":{"name":"Instagram","url":"http:\/\/instagram.com"},"url":"https:\/\/is1.4sqi.net\/pix\/4538289_RhjwghoFDF2j4A_W33nJxBXyhXSwYg6oi-cFQEMs3qc.jpg","sizes":{"count":4,"items":[{"url":"https:\/\/is1.4sqi.net\/pix\/4538289_RhjwghoFDF2j4A_W33nJxBXyhXSwYg6oi-cFQEMs3qc.jpg","width":612,"height":612},{"url":"https:\/\/is0.4sqi.net\/derived_pix\/4538289_RhjwghoFDF2j4A_W33nJxBXyhXSwYg6oi-cFQEMs3qc_300x300.jpg","width":300,"height":300},{"url":"https:\/\/is0.4sqi.net\/derived_pix\/4538289_RhjwghoFDF2j4A_W33nJxBXyhXSwYg6oi-cFQEMs3qc_100x100.jpg","width":100,"height":100},{"url":"https:\/\/is0.4sqi.net\/derived_pix\/4538289_RhjwghoFDF2j4A_W33nJxBXyhXSwYg6oi-cFQEMs3qc_36x36.jpg","width":36,"height":36}]},"user":{"id":"4538289","firstName":"Frah","lastName":"K.","photo":"https:\/\/is1.4sqi.net\/userpix_thumbs\/LAJSFTDT41E2DARV.jpg","tips":{"count":0},"gender":"female","homeCity":"Gadong, Brunei Darussalam","bio":"","contact":{}},"visibility":"public"},{"id":"50975836e4b0d8a7b4a6dd82","createdAt":1352095798,"source":{"name":"foursquare for BlackBerry","url":"https:\/\/foursquare.com\/download\/#\/blackberry"},"url":"https:\/\/is1.4sqi.net\/pix\/3324926_huWgYd7zJFhXqRVX0XGy6AHPSyVOIDrfflYFGC9cvQg.jpg","size
4

2 回答 2

2

您无法获得名称,因为它不是响应的定向子。你应该这样做

JSONObject json = (JSONObject) new JSONTokener(response).nextValue();

        JSONObject jsonResponse = json.getJSONObject("response");
        String name = jsonResponse.getJSONObject("venue").getString("name");
        Log.v("name", "v"+name);

做类似的其他领域。

于 2012-11-09T04:08:39.233 回答
0

显然,您的响应不包含任何带有“名称”键的数据。您的响应字符串看起来如何?

于 2012-11-09T01:57:23.683 回答