Http url connection for DELETE method was not passing any value in this request request.putOpt("Username", userid).I am getting proper cartid value in log but that cart id value is not passing while sending request and hereby i am attaching my code for httpclientconnection for delete method and my request too.Please give me some good solutions.Thanks in advance.
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());
}