我嘗試開發一個服務器/客戶端應用程序。服務器是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
你想使用凌空 –