无法从Foursquare获取附近的场地

时间:2012-07-08 19:49:44

标签: java android foursquare

我正在使用Foursquare API而我正在尝试获取附近的场地这是我正在使用的代码

public ArrayList<FsqVenue> SearchBykeyword(double latitude, double longitude, String query) throws Exception {
    ArrayList<FsqVenue> venueList = new ArrayList<FsqVenue>();
    try {
        String ll   = String.valueOf(latitude) + "," + String.valueOf(longitude);
        URL url     = new URL(API_URL + "/venues/search?ll=" + ll + "&query=" + query + "&radius=" + 50 + "&oauth_token=" + mAccessToken + "&v=20120610");

        Log.d(TAG, "Opening URL " + url.toString());

        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

        urlConnection.setRequestMethod("GET");
        urlConnection.setDoInput(true);

        urlConnection.connect();
        String response="";
        try{
            response = streamToString(urlConnection.getInputStream());
        } catch (Exception e) {
            String error= streamToString(urlConnection.getErrorStream());
            Log.d("error", error);
            throw e;
        }

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

        JSONArray groups    = (JSONArray) jsonObj.getJSONObject("response").getJSONArray("groups");

        int length          = groups.length();
        if (length > 0) {
            for (int i = 0; i < length; i++) {
                JSONObject group    = (JSONObject) groups.get(i);
                JSONArray items     = (JSONArray) group.getJSONArray("items");

                int ilength         = items.length();

                for (int j = 0; j < ilength; j++) {
                    JSONObject item = (JSONObject) items.get(j);

                    FsqVenue venue  = new FsqVenue();

                    venue.id        = item.getString("id");
                    venue.name      = item.getString("name");

                    JSONObject location = (JSONObject) item.getJSONObject("location");

                    Location loc    = new Location(LocationManager.GPS_PROVIDER);

                    loc.setLatitude(Double.valueOf(location.getString("lat")));
                    loc.setLongitude(Double.valueOf(location.getString("lng")));

                    venue.location  = loc;
                    //venue.address = location.getString("address");
                    venue.distance  = location.getInt("distance");
                    //venue.herenow = item.getJSONObject("hereNow").getInt("count");
                    venue.type      = group.getString("type");

                    venueList.add(venue);
                }
            }
        }
    } catch (Exception ex) {
        throw ex;
    }
    return venueList;
}

在这条线上:

urlConnection.setRequestMethod("GET");

我收到此错误:

07-08 22:45:16.432: W/System.err(27663): java.net.ProtocolException: Connection already established
07-08 22:45:16.432: W/System.err(27663):    at java.net.HttpURLConnection.setRequestMethod(HttpURLConnection.java:654)
07-08 22:45:16.440: W/System.err(27663):    at libcore.net.http.HttpsURLConnectionImpl.setRequestMethod(HttpsURLConnectionImpl.java:143)
07-08 22:45:16.440: W/System.err(27663):    at com.android.guideme.FoursquareApp.SearchBykeyword(FoursquareApp.java:207)
07-08 22:45:16.447: W/System.err(27663):    at com.android.guideme.GuideMeService$1.run(GuideMeService.java:89)

我不确定这意味着什么或问题是什么?

提前致谢

1 个答案:

答案 0 :(得分:0)

fwiw,当我第二次尝试执行请求时,我遇到了这个例外。我第一次获得了授权异常,重新进行了身份验证,并尝试使用新的oauth令牌再次运行请求。

我重用了请求对象,当时该对象无效,因为之前已经执行过了。我通过使用原始请求的方法,url和参数重新创建一个新请求并执行该请求来消除错误。