gpt4 book ai didi

java - 无法从 Foursquare 获取附近的 field

转载 作者:行者123 更新时间:2023-12-01 05:15:01 31 4
gpt4 key购买 nike

我正在使用 Foursquare API,我正在尝试获取附近的 field 这是我正在使用的代码

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)

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

提前致谢

最佳答案

fwiw,当我尝试第二次执行请求时,出现了此异常。我第一次遇到授权异常,重新进行身份验证并尝试使用新的 oauth token 再次运行请求。

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

关于java - 无法从 Foursquare 获取附近的 field ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11386132/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com