如何在android中使用截取发送Json post请求?
我的服务器以JSON格式接收Post请求,因此我创建了POJO类,在该类中设置数据,并使用GSON(toJson()方法)将其转换为JSON字符串 我正在我的应用程序中使用Volley库。如何使用Volley发送post请求? 我尝试了以下代码:如何在android中使用截取发送Json post请求?,android,json,post,gson,android-volley,Android,Json,Post,Gson,Android Volley,我的服务器以JSON格式接收Post请求,因此我创建了POJO类,在该类中设置数据,并使用GSON(toJson()方法)将其转换为JSON字符串 我正在我的应用程序中使用Volley库。如何使用Volley发送post请求? 我尝试了以下代码: Entity object = new Entity(); LinkedHashMap<String, Object> properties = new LinkedHashMap<S
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);
实体对象=新实体();
LinkedHashMap属性=新LinkedHashMap(7);
属性。放置(“电子邮件”,电子邮件);
属性。放置(“电话”,电话);
properties.put(“passwd”,password);
properties.put(“名称”,用户名);
属性。放置(“crycode”、“IN”);
object.setType(EntityType.USER);
setProperties(属性);
Gson Gson=新的Gson();
映射头=新的HashMap(1);
headers.put(“内容类型”、“应用程序/json”);
String url=“我的服务器url”;
GsonRequest myReq=新的GsonRequest(
com.android.volley.Request.Method.POST,
网址,
RegisterResponse.class,
toJson(对象),头,
createMyReqSuccessListener(),
createMyReqErrorListener());
AppController.getInstance().addToRequestQueue(myReq);
当我的服务器以JSON格式接收请求时,我是否需要将JSON字符串再次转换为JSON对象,或者转换的字符串就足够了?现在如何发送请求
请帮助我解决这个问题。您的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;
}
}
请看我忽略了userType,你也可以添加它。然后最后将它交给Gson,它将把这个类转换成JSON字符串