Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Volley: How to send JSONObject using bearer accesstoken Authentication

Tags:

android

Afternoon,

I have this code:

{
  "criteriaElement": [ 
    { 
      "Value1" : "xx",
      "FieldName" : "FieldId",
      "Operator" : 0,
      "DataSourceName" : "TableName"
    }
  ]
}

public  JSONObject crear_json_object(){
     JSONObject param= new JSONObject();
     JSONArray criteria_e= new JSONArray();
     JSONObject param_obj= new JSONObject();

     try {
         param_obj.put("Value1","xx");
         param_obj.put("Operator",0);
         param_obj.put("DataSourceName","sample_table");
         param_obj.put("FieldName","sample_field_id");
         
         criteria_e.put(param_obj);   
         param.put("criteriaelement", criteria_e);
         msgResponse.setText(param.toString());
     
     } catch(Exception _e) {}       
    return param;
}

private void get_sales_orders() {
    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
            Const.ws_Salesorders, crear_json_object(),
            new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    Log.d(TAG, response.toString());
                    msgResponse.setText(response.toString());
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d(TAG, "Error: " + error.getMessage());
                }
            }) {

        //@Override
    public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("Content-Type", "application/json");
            headers.put("Authorization", "Bearer " + access_token);
            return headers;
        }
    };
    // Adding request to request queue
    myapplication.getInstance().addToRequestQueue(jsonObjReq,
            "sales");
    
}

My jsonobject is good formed (i test this in POST extension for google chrome), and when i consume this web service in this extension just works...

But, passing it to Volley, i receive an httpstatus code 400, means that is a bad request... Somebody can help me with this??, i do not know why it does not work, am i missing something?

like image 473
Max Pinto Avatar asked Sep 19 '14 20:09

Max Pinto


1 Answers

This is what you need I presume:

JSONObject parameters = new JSONObject();
    try {
        parameters.put("key", "value");
    } catch (Exception e) {
    }
    JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, yourUrl, parameters,new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            Log.i("onResponse", response.toString());
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("onErrorResponse", error.toString());
        }
    }) {
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> headers = new HashMap<>();
            // Basic Authentication
            //String auth = "Basic " + Base64.encodeToString(CONSUMER_KEY_AND_SECRET.getBytes(), Base64.NO_WRAP);

            headers.put("Authorization", "Bearer " + accesstoken);
            return headers;
        }
    };
    queue.add(request);
like image 185
awsleiman Avatar answered Oct 03 '22 21:10

awsleiman