删除请求参数URL Volley android

时间:2018-02-09 10:31:40

标签: android api android-volley

我有一个带有删除功能的API,如下所示:

http://localhost/v1/deletePost/:id

当我尝试邮递员成功时,在URL中输入param“:id”,如.. / deletePost / 37“。

enter image description here

如何使用Volley库在android中实现“/:id”请求?

1 个答案:

答案 0 :(得分:0)

以下是关于如何使用Volley Library的guide

在发送简单请求的页面上:

final TextView mTextView = (TextView) findViewById(R.id.text);
//...

// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://www.google.com";

// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, 
new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        // Display the first 500 characters of the response string.
        mTextView.setText("Response is: "+ response.substring(0,500));
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        mTextView.setText("That didn't work!");
    }
});
// Add the request to the RequestQueue.queue.add(stringRequest);

然后您需要将GET更改为删除Request.Method.DELETE以及ID为http://localhost/v1/deletePost/37

的网址的网址

会是这样的:

String baseUrl ="http://localhost/v1/deletePost/";
String url = baseUrl + "3" //Here you change the ID, can put as variable

// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.DELETE, url, 
new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        // Display the first 500 characters of the response string.
        mTextView.setText("Response is: "+ response.substring(0,500));
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        mTextView.setText("That didn't work!");
    }
});