6

The API is developed in Wordpress with POST method. API is working fine in POSTMAN and iOS(swift). I am getting response in POSTMAN and my colleague iOS developer also getting response.

But in Android I am getting 404 error in Android Studio. I am trying to resolve with different Volley request like StringRequest, JSONObjectRequest and HttpURLConnection with AsyncTask. But getting only 404 error. Any one tell me what is the exact issue?

Below is my code.

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);
    }

I also tried with adding headers but can't get any solution.

@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;
            }

Thank you in advance.

PinkalB
  • 93
  • 6
khushbu vadi
  • 391
  • 2
  • 10
  • How about adding header in your request? [refer here](https://stackoverflow.com/questions/44000212/how-to-send-authorization-header-in-android-using-volley-library#44049327) `getHeaders()` – elbert rivas May 01 '19 at 06:22
  • 1
    @elbertrivas : I tried but not getting any response. thank you for your response. – khushbu vadi May 01 '19 at 06:24
  • Oh possible duplicate (https://stackoverflow.com/questions/26796965/android-volley-basicnetwork-performrequest-unexpected-response-code-400) – elbert rivas May 01 '19 at 06:36

2 Answers2

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", "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);
  • Try with JsonArrayRequest or StringRequest as per my code, if you are not getting JSONObject response. – Saumil Vaghela May 01 '19 at 07:02
  • not wokring with JsonObjectRequest,StringRequest,JsonArrayRequest. Thank you for your resposne. – khushbu vadi May 01 '19 at 07:18
  • Then there will be an issue is network permission setup or network library which you are using. try it will basic API call is it issues with a web service call or just with the URL that you are working with – Saumil Vaghela May 01 '19 at 09:48
0

In my case. I was using the http in my API so after changing to https it worked for me. and in postman no matter it http or https it works the same.

Shrawan Thakur
  • 829
  • 7
  • 8