我尝试开发一个服务器/客户端应用程序。服务器是glassfish,客户端android(Google http api)。Http Put Json Android
我使用休息来传输数据。
如果我做了一个Get all很好。
现在我想做一个看跌的,但我没有得到我的服务器我的内容...
客户:
JSONObject jO = new JSONObject(json);
final HttpContent content = new JsonHttpContent(new JacksonFactory(), jO);
HttpRequest request = requestFactory.buildPutRequest(url, content);
服务器:
@PUT
@Consumes(MediaType.APPLICATION_JSON)
public JsonObject putJson(JsonObject jO, @Context HttpHeaders headers){
有了休息的调试器(邮差),我可以发送一些JSON到服务器并得到它。 使用android JsonObject
进行放置(android上的内容调试为填充)。
你能帮我吗?
更新与凌空
服务器:
/**
* PUT method for updating or creating an instance of Registration
*
* @param param1
* @param jO
* @param headers
* @return
*/
@POST
@Consumes("application/json")
public JsonObject putJson(@QueryParam("email") String param1, @Context HttpHeaders headers){
Player tmpPlayer;
System.out.println(param1);
JsonObject value = Json.createObjectBuilder()
.add("firstName", "John")
.add("lastName", "Smith").build();
return value;
客户端:
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST,
URL_REGISTER, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
System.out.println(response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
System.out.println("Error");
}
}) {
@Override
public String getBodyContentType() {
return "application/json";
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json");
return headers;
}
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
JSONObject tmpObject = new JSONObject();
try {
tmpObject.put("name", "simon");
} catch (JSONException e) {
e.printStackTrace();
}
params.put("name", tmpObject.toString());
params.put("email", "[email protected]");
params.put("password", "1234");
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(registration);
requestQueue.add(request);
requestQueue.start();
感谢西蒙
你在正确的内容类型HTTP头说明您的内容填充? (在buildPutRequest()方法中的某处) –
用request.getHeaders()。setContentType(“application/json”);同样的问题 – user3657241
你想使用凌空 –