HttpURLConnection DELETE метод - PullRequest
0 голосов
/ 07 мая 2020
public static void deleteCourse(int courseId) {
        final String methodPath = "student.course/" + courseId;
        URL url = null;
        HttpURLConnection conn = null;
        String txtResult = "";
        // Making HTTP request
        try {
            url = new URL(BASE_URL + methodPath);
            //open the connection
            conn = (HttpURLConnection) url.openConnection();
             //set the connection method to GET
            conn.setRequestMethod("DELETE");
            conn.setDoOutput(true);
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setReadTimeout(10000);
            conn.setConnectTimeout(15000);
            conn.getContent();
            Log.i("Content", conn.getContent().toString());
            //PrintWriter out = new PrintWriter(conn.getOutputStream());
            //out.print(courseId);
            //conn.connect();
            Log.i("infor", "delete course" + courseId);
            //results = response.body().string();
            //PrintWriter out = new PrintWriter(conn.getOutputStream());
            //out.print(stringCourseJson);
           // Log.i("infor", stringCourseJson);
            //out.flush();
            //out.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            conn.disconnect();
        }
    }

Я создаю приложение android и использую HttpURL для подключения к веб-службе REST. Однако все методы (PUT GET POST) работают, кроме удаления. Я перепробовал все способы. Единственный работоспособный метод - добавить conn.getContent () после открытия соединения. Дело в том, что я не понимаю, зачем нужен этот код, на мой взгляд, он совершенно не связан. Спасибо.

...