java.io.FileNotFoundException
api时 /tips/add
。这是我的源代码:
public void addTip(final String venueId, final String text) {
new Thread() {
@Override
public void run() {
try {
URL url = new URL(FOURSQUARE_API
+ "/tips/add"
+ "?venueId=" + venueId
+ "&text=" + text
+ "&oauth_token=" + mAccessToken
+ "&v=" + mVersion);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
Log.d(TAG, "opening url " + url);
urlConnection.setRequestMethod("POST");
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.connect();
try {
JSONObject response = getResponse(urlConnection);
String tipId = response.getString("id");
String text = response.getString("text");
Log.d(TAG, "tipId:" + tipId + "; " + "text:" + text);
} catch (Exception e) {
String response = streamToString(urlConnection.getErrorStream());
Log.d(TAG, response);
throw e;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
private JSONObject getResponse(HttpURLConnection urlConnection) throws Exception {
String response = streamToString(urlConnection.getInputStream());
JSONObject jsonObject = (JSONObject) new JSONTokener(response).nextValue();
JSONObject resp = jsonObject.getJSONObject("response");
return resp;
}
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;
}
和错误信息:
12-24 21:31:18.758: W/System.err(2356): java.io.FileNotFoundException: https://api.foursquare.com/v2/tips/add?venueId=4ef2cdd0b634355dee1f1794&text=classroom for sei students&oauth_token=CONS3IBOUEH2YTBA3IMNI3YH4CWSUKQ500QSOXJZQ23LXBH1&v=20111224
12-24 21:31:18.768: W/System.err(2356): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:521)
12-24 21:31:18.768: W/System.err(2356): at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:258)
12-24 21:31:18.768: W/System.err(2356): at com.ecnu.sei.manuzhang.study.FoursquareAPI.getResponse(FoursquareAPI.java:339)
12-24 21:31:18.768: W/System.err(2356): at com.ecnu.sei.manuzhang.study.FoursquareAPI.access$7(FoursquareAPI.java:338)
12-24 21:31:18.768: W/System.err(2356): at com.ecnu.sei.manuzhang.study.FoursquareAPI$2.run(FoursquareAPI.java:133)
奇怪的是,我在捕获异常后从getErrorStream()
得不到任何东西。
有任何想法吗?您可以找到文档here
答案 0 :(得分:3)
您需要对text
位进行编码。试试这个:
+ "&text=" + URLEncoder.encode(text,"UTF-8");