【问题标题】:How to send a Json post request using volley in android?如何在 android 中使用 volley 发送 Json 发布请求?
【发布时间】:2015-05-16 10:29:51
【问题描述】:

我的服务器接受 JSON 格式的 Post 请求,所以我创建了 POJO 类,我在其中设置数据并使用 GSON(toJson() 方法)转换为 JSON 字符串

我在我的应用程序中使用 Volley 库。如何使用 volley 发送帖子请求? 我尝试了以下代码:

 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);

当我的服务器接受 JSON 格式的请求时,我需要将 JSON 字符串再次转换为 JSON 对象还是我转换的字符串就足够了?以及现在如何发送请求?

请帮我解决这个问题。

【问题讨论】:

    标签: android json post gson android-volley


    【解决方案1】:

    您的 POJO 类应如下所示:

        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;
        }
    }
    

    请看我忽略了用户类型,你也可以添加它。最后将它交给 Gson,它会将这个 Class 转换为 JSON String。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-16
      • 1970-01-01
      • 2017-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-09
      相关资源
      最近更新 更多