【问题标题】:How to fix "Volley: [15771] BasicNetwork.performRequest: Unexpected response code 404 in android?如何修复“Volley:[15771] BasicNetwork.performRequest:Android 中的意外响应代码 404?
【发布时间】:2019-05-01 06:12:56
【问题描述】:

API 是在 Wordpress 中使用 POST 方法开发的。 API 在 POSTMAN 和 iOS(swift) 中运行良好。我收到了 POSTMAN 的回复,我的同事 iOS 开发人员也收到了回复。

但在 Android 中,我在 Android Studio 中遇到 404 错误。 我正在尝试使用 AsyncTask 解决不同的 Volley 请求,例如 StringRequest、JSONObjectRequest 和 HttpURLConnection。但只得到 404 错误。有人告诉我确切的问题是什么吗?

下面是我的代码。

private void RegisterUser(){
        final ProgressDialog progressDialog = new ProgressDialog(RegistrationActivity.this);
        progressDialog.setCancelable(false);
        progressDialog.setMessage("Please wait...");
        progressDialog.show();

        StringRequest postrequest = new StringRequest(Request.Method.POST, Urls.REGISTER, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    progressDialog.dismiss();
                    Log.e("res","==> "+response);
                }
                catch (Exception e){e.printStackTrace();}
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {progressDialog.dismiss();error.getLocalizedMessage();}
        }) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("username", "khushbu");
                params.put("email", "kh@test.com");
                params.put("user_pass", "test@123");
                params.put("display_name", "khushbu");
                params.put("company_name", "");
                params.put("nature_of_business", "");
                params.put("country", "");
                params.put("nonce", "12e099a946");
                params.put("notify", "both");
                params.put("insecure", "cool");

                Log.e("params","==> " + params);
                return params;
            }
        };
        FlawlessApplication.getInstance().addToRequestQueue(postrequest);
    }

我也尝试过添加标题,但没有得到任何解决方案。

@Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String>  params = new HashMap<String, String>();
                params.put("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
                params.put("cache-control", "no-cache");

                return params;
            }

提前谢谢你。

【问题讨论】:

标签: java android wordpress-rest-api


【解决方案1】:
final ProgressDialog progressDialog;
progressDialog = ProgressDialog.show(mContext, "", "Loading..");
progressDialog.setCancelable(false);
progressDialog.show();

RequestQueue requestQueue = Volley.newRequestQueue(mContext);
HashMap<String, String> params = new HashMap<>();
params.put("username", "khushbu");
params.put("email", "kh@test.com");
params.put("user_pass", "test@123");
params.put("display_name", "khushbu");
params.put("company_name", "");
params.put("nature_of_business", "");
params.put("country", "");
params.put("nonce", "12e099a946");
params.put("notify", "both");
params.put("insecure", "cool");
Log.e("params","==> " + params);

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
    Request.Method.POST,
    Urls.REGISTER,
    new JSONObject(params),
    new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            try {
                progressDialog.dismiss();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                Log.e("res","==> "+response);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    },
    new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            // Do something when error occurred
            try {
                progressDialog.dismiss();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
) {
    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        HashMap<String, String> headerParams = new HashMap<>();
        //add header params if having
        headerParams.put("KEY", "value");
        return headerParams;
    }
};
// Add JsonObjectRequest to the RequestQueue
requestQueue.add(jsonObjectRequest);

【讨论】:

  • 如果您没有收到 JSONObject 响应,请按照我的代码尝试使用 JsonArrayRequest 或 StringRequest。
  • 不适用于 JsonObjectRequest、StringRequest、JsonArrayRequest。感谢您的回复。
  • 那么会有一个问题是您正在使用的网络权限设置或网络库。尝试一下基本的 API 调用是 Web 服务调用问题还是只是您正在使用的 URL 问题
【解决方案2】:

就我而言。 我在我的 API 中使用 http,所以在更改为 https 后它对我有用。在邮递员中,无论是 http 还是 https,它的工作原理都是一样的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    相关资源
    最近更新 更多