I’ve been trying to use the venues/add api as in the following codes:
private static final String FOURSQUARE_API = "https://api.foursquare.com/v2"
public void addVenue(final String address, final String name) {
new Thread() {
@Override
public void run() {
try {
URL url = new URL(FOURSQUARE_API
+ "/venues/add"
+ "?name=" + address + PREFIX + name
+ "&ll=" + mLL
+ "&oauth_token=" + mAccessToken
+ "&v=" + mVersion);
Log.d(TAG, "opening url " + url);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.connect();
String response = streamToString(urlConnection.getInputStream());
JSONObject jsonObject = (JSONObject) new JSONTokener(response).nextValue();
JSONObject resp = jsonObject.getJSONObject("response");
JSONObject venue = resp.getJSONObject("venue");
String venueId = venue.getString("id");
String name = venue.getString("name");
JSONObject location = venue.getJSONObject("location");
String lat = location.getString("lat");
String lng = location.getString("lng");
Log.d(TAG, "venueId: " + venueId + "; " + "name: " + name + "; " + "latitude: " + lat + "longitude:" + lng);
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
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;
}
mLL is user’s current position and address + PREFIX + name is user-specified name
I know if I attempt to add two similar names I may get an error as stated here. However, what I encounter is a java.io.FileNotFoundException which makes it impossible to resubmit. I’ve successfully added new venues with distinguished names. So what is this exception about?
EDIT: here’s the complete Error info:
12-23 10:17:41.042: W/System.err(377): java.io.FileNotFoundException: https://api.foursquare.com/v2/venues/add?name=LikeLouclassroomB222&ll=31.2297,121.4033&oauth_token=CONS3IBOUEH2YTBA3IMNI3YH4CWSUKQ500QSOXJZQ23LXBH1&v=20111223
12-23 10:17:41.042: W/System.err(377): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:521)
12-23 10:17:41.062: W/System.err(377): at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:258)
12-23 10:17:41.072: W/System.err(377): at com.ecnu.sei.manuzhang.study.FoursquareAPI$1.run(FoursquareAPI.java:56)
(FoursquareAPI.java:56) => String response = streamToString(urlConnection.getInputStream());
p.s. I really need to add venues with similar names.
I opened the code of HttpURLConnectionImpl that throws the exception.
That exception is thrown if the response code from the server is >= HTTP_BAD_REQUEST (=400)
(By the way, this is the only thing that throws a FileNotFoundException in HttpURLConnectionImpl)
Since you did not give us the exact response code, as akdotcom suggested look at the responses that foursquare can return , use getErrorStream() to get the data that the foursquare server returned. It should clarify the issue..