【问题标题】:Volley POST Request: 'com.android.volley.ClientError'Volley POST 请求:'com.android.volley.ClientError'
【发布时间】:2021-10-15 13:55:46
【问题描述】:

我正在制作一个视频播放器应用程序,并希望使用“打开字幕 api”从应用程序中下载字幕,就像“VLC 媒体播放器”一样。触发下载 API 时,Volley 在发出带有三个标头和一个参数的 POST 请求时会给出此错误。 API 工作正常,当我运行我的应用程序时它会被触发。我已经在邮递员上进行了尝试和测试,它工作正常并给出此响应 但是我没有在 android 应用程序中收到此响应,而是“com.android.volley.ClientError”,我被困在这里。

这是我的代码:

 public void sendDownloadRequest()  {
    
    StringRequest downloadRequest = new StringRequest(Request.Method.POST, downloadurl,  new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {

            Toast.makeText(context, "Response: " + response, Toast.LENGTH_SHORT).show();
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
            Toast.makeText(context, "Error from Download: " + error.toString(), Toast.LENGTH_LONG).show();
        }
    }) {

        @Nullable
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> bodyParams = new HashMap<>();
            bodyParams.put("file_name", "batman");
            return bodyParams;
        }

        @Nullable
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> headerParams = new HashMap<>();
            headerParams.put("Authorization", "My Auth Key");
            headerParams.put("Api-Key", "My Api Key");
            headerParams.put("Content-Type", "application/x-www-form-urlencoded");
            return headerParams;
        }
    };

    requestQueue.add(downloadRequest);

这是邮递员的请求正文

我认为这可能是参数的错误,但我已经给出了正确的参数 任何帮助将不胜感激。

【问题讨论】:

  • 如果Authorization 类型是Bearer Token,您还必须将前缀Bearer 添加到您的My Auth Key
  • 授权工作正常,因为我的 API 被触发,但我得到的响应不正确

标签: android api android-studio postman android-volley


【解决方案1】:

也尝试覆盖getBodyContentType()

...

public String getBodyContentType() {
    return "application/x-www-form-urlencoded; charset=UTF-8";
}

protected Map<String, String> getParams() {

...

并删除 Content-Type 标头。

【讨论】:

  • 删除了 Content-Type 标头 并覆盖了 getBodyContentType() 但还是同样的错误!
  • 将postman中的请求正文截图上传到问题中
猜你喜欢
  • 2021-03-11
  • 2019-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多