How can I send a JSON string request or post in the webservice with authorization header using volley?

588 Views Asked by At

I have a previous code for connecting to a webservice using httpclient. String entity it is working with no problem. I want to update it using volley. I tried the below code but it gives me unexpected, BasicNetwork.performRequest: Unexpected response code 400.

JSONObject jsonObject = null;

    try {
        jsonObject = new JSONObject("{\"LoginDate\":\"\\/Date(" + today.getTime() + "+0800)\\/\",\"MAC\":\"00-00-00-12-12-12\",\"MerchantID\":\"xxxxx\",\"StationName\":\"WKSTN01\",\"UserName\":\"exampleuser\"}");
    }catch (Exception e) {
        e.printStackTrace();
    }

    Log.e("JSONOBEJCT", jsonObject.toString());

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, URL, jsonObject , new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject jsonObject) {


            Log.e("Response", jsonObject.toString());
            pDialog.hide();
        }

    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

            VolleyLog.d("Error: " + error.getMessage());
            pDialog.hide();

        }

    }) {

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<>();
            headers.put("Content-Type", "application/json");
            headers.put("Authorization", messageHasValue);
            return headers;
        }
    };

This is my old code and I just want to update it with volley, is there any way I can do it?...

StringEntity entity = new StringEntity(object.toString());
            HttpClient client = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(URL);

            entity.setContentType("application/json");
            httpPost.addHeader("Authorization", messageHasValue);

            httpPost.setEntity(entity);

            Log.e("HTTPPOST ", httpPost.toString());

            HttpResponse response = client.execute(httpPost);
            HttpEntity resEntity = response.getEntity();
1

There are 1 best solutions below

0
On

Its working now, I tried to upgrade my PHP version from 4 to 5 and format the TimeStamp.