【问题标题】:Delete request params URL Volley android删除请求参数 URL Volley android
【发布时间】:2018-02-09 10:31:39
【问题描述】:

我有一个带有这样删除功能的 API:

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

当我尝试在邮递员中通过在 url ":id" 中输入参数成功时,例如 ../deletePost/37"。

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

【问题讨论】:

  • 您可以简单地将 id 与String url = "http://localhost/v1/deletePost"; String id = "/"+3 String finalUrl =url+id; 之类的 URL 连接起来
  • 你有示例代码吗?
  • 我试过后,他的回复发出400错误:02-10 11:16:12.877 25325-25368 /? E / Volley:[4113] BasicNetwork.performRequest:192.168.43.193/v1/deletePost/37@UpendraShah 的意外响应代码 400
  • String DELETEADD = "192.168.43.193/v1/deletePost String id = "/"+ id_post; String finalUrl = DELETEADD + id;
  • 请检查我的更新问题,我添加了照片@UpendraShah

标签: android api android-volley


【解决方案1】:

这里是guide关于如何使用 Volley Library

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

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 更改为 DELETE Request.Method.DELETE 并将您的 url 的 url 更改为 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!");
    }
});

【讨论】:

  • 我在文档中没有找到
  • 好吧,我会试试@Canato
猜你喜欢
  • 1970-01-01
  • 2016-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多