Android Volley post json data to server
Asked Answered
E

3

5

I am new in Java. I want to send post json data to webserver. My Volley post is given below.

public void postData(String url,JSONObject data,final VolleyCallback mResultCallback){
    RequestQueue requstQueue = Volley.newRequestQueue(mContext);

    JsonObjectRequest jsonobj = new JsonObjectRequest(Request.Method.POST, url,null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    if(mResultCallback != null){
                        mResultCallback.notifySuccess(response);
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    if(mResultCallback != null){
                        mResultCallback.notifyError(error);
                    }
                }
            }
    ){
      //here I want to post data to sever
    };
    requstQueue.add(jsonobj);

}

Here is my MainActivity code

JSONObject data = null;
            try {
                String datas = "{'email': email,'password': password}";
                data = new JSONObject(datas);
            }catch (JSONException e){
                e.printStackTrace();
            }
String url = "http://example.com";

I want to post JSON data to my PostData method. How can I post this json data to my server?

Emotionalize answered 24/6, 2017 at 4:18 Comment(1)
new JsonObjectRequest(Request.Method.POST, url,null, third params if for JSONObject params. replace null with your jsondataBarty
C
11
    public void postData(String url,Hashmap data,final VolleyCallback mResultCallback){
        RequestQueue requstQueue = Volley.newRequestQueue(mContext);

        JsonObjectRequest jsonobj = new JsonObjectRequest(Request.Method.POST, url,new JSONObject(data),
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        if(mResultCallback != null){
                            mResultCallback.notifySuccess(response);
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        if(mResultCallback != null){
                            mResultCallback.notifyError(error);
                        }
                    }
                }
        ){
          //here I want to post data to sever
        };
        requstQueue.add(jsonobj);

    }

Now, from your mainActiviy class

    Hashmap data = new HashMap();
    data.put("email","email");
    data.put("password","password");      
    String url = "http://example.com";

//now you can just call the method, I have rectified your string to hashmap,
postData(url,data,new mResultCallb.....              //rest of your code
Catechetical answered 24/6, 2017 at 4:32 Comment(0)
L
3
public void postData(String url,JSONObject data,final VolleyCallback mResultCallback){
RequestQueue requestQueue = Volley.newRequestQueue(mContext);

JsonObjectRequest jsonobj = new JsonObjectRequest(Request.Method.POST, url,data,
        new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                if(mResultCallback != null){
                    mResultCallback.notifySuccess(response);
                }
            }
        },
        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                if(mResultCallback != null){
                    mResultCallback.notifyError(error);
                }
            }
        }
);
requstQueue.add(jsonobj);

}
Lowelllowenstein answered 24/6, 2017 at 4:44 Comment(0)
D
0
public void postData(String url,final JSONObject data,final VolleyCallback mResultCallback){
    RequestQueue requstQueue = Volley.newRequestQueue(mContext);

    JsonObjectRequest jsonobj = new JsonObjectRequest(Request.Method.POST, url,null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    if(mResultCallback != null){
                        mResultCallback.notifySuccess(response);
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    if(mResultCallback != null){
                        mResultCallback.notifyError(error);
                    }
                }
            }
    ){
       @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                if(data != null){
                    Iterator<String> keysItr = data.keys();
                    while(keysItr.hasNext()) {
                            String key = keysItr.next();
                            Object value = data.get(key);

                            if(value instanceof JSONArray) {
                                value = toList((JSONArray) value);
                            }

                            else if(value instanceof JSONObject) {
                                value = toMap((JSONObject) value);
                            }
                            params.put(key, value);
                        }
                } 
                return params;
            }
    };
    requstQueue.add(jsonobj);

this is working code for me

Hope this might be work for you..

Happy Codding

Diplostemonous answered 24/6, 2017 at 4:42 Comment(2)
this doesn't solve the requirement (passing null in JSON OBJ)Ung
my Android Studion didn't resolve data.keys() , toList((JSONArray) value); and toMap((JSONObject) value); methodEmotionalize

© 2022 - 2024 — McMap. All rights reserved.