0

I can POST Map <String, String> to my server but it comes in & separated form.

I have used code from Send post request using Volley and receive in PHP

getParams() just doesn't work with JSONObject return type. Is it possible to send JSONObject as JSON only?

I want to send data as JSON that I will get using file_get_contents(php://input). For this I have changed Content-Type to application/json; charset=utf-8.

The problem is using this way I get data in format of x=abc&y=def as it's Map<String, String> type and I want data in JSON format of {"x":"abc", "y":"def"}

It's different from above question because I want to POST data in JSON ONLY and not in MAP of String

Community
  • 1
  • 1
Himanshu Shankar
  • 735
  • 1
  • 6
  • 24

2 Answers2

0

Try this :

private void jsonObjReq() {

        showProcessDialog();
        Map<String, String> postParam= new HashMap<String, String>();
        postParam.put("un", "xyz@gmail.com");
        postParam.put("p", "somepasswordhere");



JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
        Const.URL_LOGIN, new JsonObject(postParam),
        new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                Log.d(TAG, response.toString());
                msgResponse.setText(response.toString());
                hideProgressDialog();
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
                hideProgressDialog();
            }
        }) {

    /**
     * Passing some request headers
     * */
    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        HashMap<String, String> headers = new HashMap<String, String>();
        headers.put("Content-Type", "application/json; charset=utf-8");
        return headers;
    }



};

change header in your php too.

Himanshu Shankar
  • 735
  • 1
  • 6
  • 24
Ashwani
  • 1,294
  • 1
  • 11
  • 24
0

the easy way is this

final RequestQueue requestQueue = Volley.newRequestQueue(this);
final String url ="http://mykulwstc000006.kul/Services/Customer/Register";
Map<String, String>  params = new HashMap<String, String>();
params.put("MobileNumber", "+97333765439");
params.put("EmailAddress", "danish.hussain4@das.com");
params.put("FirstName", "Danish2");
params.put("LastName", "Hussain2");
params.put("Country", "BH");
params.put("Language", "EN");
JsonObjectRequest req = new JsonObjectRequest(url, new JSONObject(params),
        new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    VolleyLog.v("Response:%n %s", response.toString(4));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        VolleyLog.e("Error: ", error.getMessage());
    }
}){
    @Override
    public String getBodyContentType() {
        return "application/json; charset=utf-8";
    }

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        String username ="eli@gmail.com";
        String password = "elie73";

        String auth =new String(username + ":" + password);
        byte[] data = auth.getBytes();
        String base64 = Base64.encodeToString(data, Base64.NO_WRAP);
        HashMap<String, String> headers = new HashMap<String, String>();
        headers.put("Authorization","Basic "+base64);
        return headers;
    }

};
requestQueue.add(req);

}

Syed Danish Haider
  • 1,334
  • 11
  • 15