添加新场地时java.io.FileNotFoundException

时间:2011-12-22 13:27:38

标签: android foursquare

我一直在尝试使用以下代码中的venues/add API:

    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是用户的当前位置,address + PREFIX + name是用户指定的名称
  我知道如果我尝试添加两个相似的名称,我可能会收到here所述的错误。但是,我遇到的是java.io.FileNotFoundException,因此无法重新提交。我成功地添加了具有杰出名称的新场地。那么这个例外是什么呢?

编辑:这是完整的错误信息:

 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。我真的需要添加名字相似的场地。

1 个答案:

答案 0 :(得分:2)

我打开了抛出异常的HttpURLConnectionImpl代码。

如果来自服务器的响应代码是> = HTTP_BAD_REQUEST(= 400),则抛出该异常 (顺便说一句,这是在HttpURLConnectionImpl中抛出FileNotFoundException的唯一方法)

由于您未向我们提供确切的响应代码,因为akdotcom建议查看responses that foursquare can return,请使用getErrorStream()获取foursquare服务器返回的数据。它应该澄清这个问题。