我正在开发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?meetingId = _venue id]& text = [utf-8 encoded text]& 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响应代码。
答案 0 :(得分:1)
执行POST时,参数不应该是URL的一部分(将它们指定为POST的参数)。
答案 1 :(得分:0)
我解决了这个问题。
private void methodTipAdd3(String venueId, String tip) {
String url = "https://api.foursquare.com/v2/tips/add";
StringBuilder sb = new StringBuilder();
sb.append("oauth_token=");
sb.append(getAccessToken()).append('&');
try{
sb.append("venueId").append('=');
sb.append(URLEncoder.encode(venueId, "UTF-8")).append('&');
}catch(Exception e) {
e.printStackTrace();
}
try{
sb.append("text").append('=');
sb.append(URLEncoder.encode(tip, "UTF-8")).append('&');
}catch(Exception e) {
e.printStackTrace();
}
sb.append("v=" + getVersion());
String params = sb.toString();
String result = null;
int httpcode = 200;
try {
URL aUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) aUrl.openConnection();
try {
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Accept", "application/json");
byte buf[] = params.getBytes("UTF-8");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", String.valueOf(buf.length));
connection.setDoOutput(true);
OutputStream outputstream = connection.getOutputStream();
outputstream.write(buf);
outputstream.flush();
outputstream.close();
httpcode = connection.getResponseCode();
if (httpcode == 200) {
InputStream inputStream = connection.getInputStream();
result = convertStreamToString(inputStream);
// handle tip
android.util.Log.e(tag, "result: " + result);
} else {
android.util.Log.e(tag, "http response code: " + httpcode);
}
} finally {
connection.disconnect();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}