简体   繁体   中英

How to send a Json post request using volley in android?

My server takes Post request in JSON format so i have created POJO class where i set data to it and converted to JSON string using GSON (toJson() method)

I am using Volley library in my application.How to send post request using volley? i tried the following code:

 Entity object = new Entity();
                        LinkedHashMap<String, Object> properties = new LinkedHashMap<String, Object>(7);
                        properties.put("email",email);
                        properties.put("phone",phone);
                        properties.put("passwd",password);
                        properties.put("name",username);
                        properties.put("crycode","IN");
                        object.setType(EntityType.USER);
                        object.setProperties(properties);
                        Gson gson = new Gson();


                        Map<String,String> headers = new HashMap<String, String>(1);
                        headers.put("Content-Type", "application/json");

                        String url = "my server URL";
 GsonRequest<RegisterResponse> myReq = new GsonRequest<RegisterResponse>(
                                com.android.volley.Request.Method.POST,
                                url,
                                RegisterResponse.class,
                                gson.toJson(object),headers,
                                createMyReqSuccessListener(),
                                createMyReqErrorListener());

                    AppController.getInstance().addToRequestQueue(myReq);

As my server takes request in JSON format do i need to convert the JSON string again into JSON object or the string i converted is sufficient? and how to send the request now?

Please help me in solving this issue.

Your POJO class should look something like this:

    public class Entity {
    private String email;
    private String phone;
    private String passwd;
    private String name;
    private String crycode;

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getPasswd() {
        return passwd;
    }

    public void setPasswd(String passwd) {
        this.passwd = passwd;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCrycode() {
        return crycode;
    }

    public void setCrycode(String crycode) {
        this.crycode = crycode;
    }
}

Please see i ignored userType you can add that too. And then finally give it to Gson it will convert this Class to JSON String.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM