1

我正在使用 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)

我不确定这意味着什么或有什么问题有什么想法?

提前致谢

4

1 回答 1

0

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

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

于 2013-04-20T20:09:52.760 回答