我正在开发 Android 中的foursquare API v2。
在我的应用程序中,用户可以签到并添加小费。
签入方法运行良好,但添加提示方法出错。
private void methodTipAdd(String venueId, String tip, boolean auth) {
StringBuilder urlBuilder = new StringBuilder("https://api.foursquare.com/v2/");
urlBuilder.append("tips/add");
urlBuilder.append('?');
try{
urlBuilder.append("venueId").append('=');
urlBuilder.append(URLEncoder.encode(venueId, "UTF-8")).append('&');
}catch(Exception e) {
e.printStackTrace();
}
try{
urlBuilder.append("text").append('=');
urlBuilder.append(URLEncoder.encode(tip, "UTF-8")).append('&');
}catch(Exception e) {
e.printStackTrace();
}
if (auth) {
urlBuilder.append("oauth_token=");
urlBuilder.append(getAccessToken());
} else {
urlBuilder.append("client_id=");
urlBuilder.append(CLIENT_ID);
urlBuilder.append("&client_secret=");
urlBuilder.append(CLIENT_SECRET);
}
urlBuilder.append("&v=" + getVersion());
String url = urlBuilder.toString();
String result = null;
try {
URL aUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) aUrl.openConnection();
try {
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.connect();
int code = connection.getResponseCode();
if (code == 200) {
InputStream inputStream = connection.getInputStream();
result = convertStreamToString(inputStream);
android.util.Log.e(tag, "result: " + result);
// handle tip
} else {
android.util.Log.e(tag, "HttpURLConnection response code: " + code);
}
} finally {
connection.disconnect();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
请求网址:https://api.foursquare.com/v2/tips/add?venueId=[venue id]&text=[utf-8 编码文本]&oauth_token=[my_oauth_token]&v=20120730
ex) https://api. foursquare.com/v2/tips/add?venueId=XXX123YYY&text=Good&oauth_token=XXX123YYY&v=20120730
http响应码:400
我想知道为什么我得到 HTTP_BAD_REQUEST 响应代码。