删除方法的HttpUrlConnection是不是接受任何参数?

时间:2017-03-31 08:21:33

标签: android http httpurlconnection

DELETE 方法的Http网址连接未在此请求中传递任何值 request.putOpt(“用户名”,用户ID)。我正在使用 cartid 日志中的值,但购物车ID 值未在发送请求时传递,因此我附加了我的代码用于httpclientconnection的删除方法和我的请求。请给我一些很好的解决方案。谢谢提前。

private static JSONObject delete(String sUrl, String body) {

    HttpURLConnection connection = null;

    String authentication = "dem" + ":" + "dem123";
    String encodedAuthentication = Base64
        .encodeToString(authentication.getBytes(), Base64.NO_WRAP);

    try {
        URL url = new URL(sUrl);
        connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setRequestMethod("DELETE");
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setRequestProperty("Accept", "application/json");
        connection.setRequestProperty("Authorization",
            "Basic " + encodedAuthentication);
        connection.setRequestProperty("Accept-Charset", "utf-8,*");
        OutputStreamWriter streamWriter = new OutputStreamWriter(
            connection.getOutputStream());

        streamWriter.write(body);
        streamWriter.flush();
        StringBuilder stringBuilder = new StringBuilder();
        if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            InputStreamReader streamReader = new InputStreamReader(
                connection.getInputStream());
            BufferedReader bufferedReader = new BufferedReader(
                streamReader);
            String response = null;
            while ((response = bufferedReader.readLine()) != null) {
                stringBuilder.append(response + "\n");
            }
            bufferedReader.close();


            return new JSONObject(stringBuilder.toString());
        } else {
            Log.d("Put-Error", connection.getResponseMessage());
            return null;
        }
    } catch (Exception exception) {
        Log.e("Put-Exception", exception.toString());
        return null;
    } finally {
        if (connection != null) {
            connection.disconnect();
        }
    }
}


 public static JSONObject ClearcartItems(Context ctx, String sUrl,
                                        String userid) throws   JSONException, IOException {
    JSONObject request = new JSONObject();
    Log.e("userid",""+userid);//I am getting cartid value here
    request.putOpt("Username", userid);
    sUrl = sUrl + "clear-all-cart-items";
    Log.e("del", "" + sUrl);
    return delete(sUrl, request.toString());
}

1 个答案:

答案 0 :(得分:0)

RequestMethod" DELETE"不支持RequestBody