如何修复“Volley:[15771] BasicNetwork.performRequest:android中意外的响应代码404?

问题描述 投票:5回答:1

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

但在Android中我在Android Studio中收到404错误。我试图解决不同的Volley请求,如StringRequest,JSONObjectRequest和HttpURLConnection与AsyncTask。但只得到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", "[email protected]");
                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个回答
0
投票
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", "[email protected]");
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);
© www.soinside.com 2019 - 2024. All rights reserved.