2014-06-11 88 views
-1

我正在构建一个Android应用程序。我试图从获取数据https://www.googleapis.com/freebase/v1/searchFreeBase Android查询返回仅1结果

代码:

query = "'[{'" + 
         "'name': null,'" + 
         "'mid': null,'" + 
         "'/location/location/geolocation': [{'" + 
          "'longitude': null,'" + 
          "'latitude': null'" + 
          "'}],'" + 
        "'type': '/travel/tourist_attraction'" + 
        "'}]'".replace('\'', '"');; 

       nameValuePairs.add(new BasicNameValuePair("query", query)); 
       nameValuePairs.add(new BasicNameValuePair("limit", "10")); 
       nameValuePairs.add(new BasicNameValuePair("indent", "true")); 
       nameValuePairs.add(new BasicNameValuePair("key", FreeBase.API_KEY)); 

       // Making a request to url and getting response 
       String jsonStr = sh.makeServiceCall(url, FreeBaseServiceHandler.GET, nameValuePairs); 


public String makeServiceCall(String url, int method, 
      List<NameValuePair> params) { 
     try { 
      // http client 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpEntity httpEntity = null; 
      HttpResponse httpResponse = null; 

      // Checking http request method type 
      if (method == POST) { 
       HttpPost httpPost = new HttpPost(url); 
       // adding post params 
       if (params != null) { 
        httpPost.setEntity(new UrlEncodedFormEntity(params)); 
       } 

       httpResponse = httpClient.execute(httpPost); 

      } else if (method == GET) { 
       // appending params to url 
       if (params != null) { 
        String paramString = URLEncodedUtils 
          .format(params, "utf-8"); 
        url += "?" + paramString; 
       } 
       HttpGet httpGet = new HttpGet(url); 

       httpResponse = httpClient.execute(httpGet); 

      } 
      httpEntity = httpResponse.getEntity(); 
      response = EntityUtils.toString(httpEntity); 
      Log.v("test", response); 
     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     return response; 

    } 

它应该给我所有的旅游景点,他们的地理位置等,像下面的结果:

{ 
    "result": [ 
    { 
     "/location/location/geolocation": [{ 
     "latitude": 37.826944, 
     "longitude": -122.4225 
     }], 
     "type": "/travel/tourist_attraction", 
     "mid": "/m/0h594", 
     "name": "Alcatraz Island" 
    }, 
    { 
     "/location/location/geolocation": [{ 
     "latitude": 27.174167, 
     "longitude": 78.041944 
     }], 
     "type": "/travel/tourist_attraction", 
     "mid": "/m/0l8cb", 
     "name": "Taj Mahal" 
    }] 

而是将其给我一个页面中间的结果(https://www.freebase.com/m/010gxlnv)包含一个旅游景点的所有信息。

06-11 22:00:52.776: D/Response:(774):  { 
06-11 22:00:52.776: D/Response:(774):  "mid": "/m/010gxlnv", 
06-11 22:00:52.776: D/Response:(774):  "name": "Tourist Attraction LatLongs", 
06-11 22:00:52.776: D/Response:(774):  "lang": "en", 
06-11 22:00:52.776: D/Response:(774):  "score": 169.224823 
06-11 22:00:52.776: D/Response:(774):  } 
06-11 22:00:52.776: D/Response:(774): ], 
06-11 22:00:52.776: D/Response:(774): "cost": 5, 
06-11 22:00:52.776: D/Response:(774): "hits": 1 
06-11 22:00:52.776: D/Response:(774): } 

你知道我的代码有什么问题吗?

+0

任何想法为什么这是一个坏问题? – wvdz

回答

0

此问题已解决。我使用googleapis.com/freebase/v1/mqlread而不是googleapis.com/freebase/v1/search