0

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();
Cyd
  • 1,245
  • 1
  • 14
  • 17
  • HTTP 400 status code means the request could not be understood by the server due to malformed syntax. Can you check your request first using a curl or a rest client? – random Jun 13 '15 at 05:50
  • Hi thank you for reply, this is what i have before, HttpClient client = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(URL); entity.setContentType("application/json"); httpPost.addHeader("Authorization", messageHasValue); httpPost.setEntity(entity); HttpResponse response = client.execute(httpPost); HttpEntity resEntity = response.getEntity(); and this is working fine, I just want to update it to volley. any idea how can I do this?.. – Cyd Jun 15 '15 at 03:00
  • Your request headers seem to be correctly set. Can you try creating your jsonObject like this: JSONObject jsonObject = new JSONObject(); jsonObject.put("LoginDate",your_date_string); and so on? – random Jun 15 '15 at 04:30
  • HI random, I have tried it and the result is the same, I also used the override getparams. I dont know what is the issue there, but the old code is working fine with the StringEntity.. – Cyd Jun 15 '15 at 14:07
  • Try something as suggested here http://stackoverflow.com/questions/22920039/how-to-use-setentity-in-android-volley – random Jun 16 '15 at 06:06

1 Answers1

0

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

Cyd
  • 1,245
  • 1
  • 14
  • 17