【发布时间】: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