0

我正在开发 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 响应代码。

4

2 回答 2

1

进行 POST 时,参数不应成为 URL 的一部分(将它们指定为 POST 的参数)。

于 2012-07-30T09:26:40.763 回答
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();
    }
}
于 2012-07-31T08:15:22.267 回答