我是开发RESTful Web服务的新手。使用POST-Request我想创建一个Author
类型的实体。我从Android智能手机通过Android Volley调用该服务,并始终收到HTTP错误400 Bad Request
。
Request-Body中的JSON是有效的,如果我在Chrome内部使用Postman测试它,web服务就可以工作。
实体类:
@Entity
@XmlRootElement
public class Author implements Serializable {
@Id
@XmlElement
private String eMail;
@XmlElement
private String password;
//GETTER & SETTER
}
生成的网络服务:
@Stateless
@Path("author")
public class AuthorFacadeREST extends AbstractFacade<Author> {
@PersistenceContext(unitName = "Fremdenfuehrer_PU")
private EntityManager em;
public AuthorFacadeREST() {
super(Author.class);
}
@POST
@Override
@Consumes(MediaType.APPLICATION_JSON)
public void create(Author entity) {
super.create(entity);
}
}
使用Android Volley请求:
RequestQueue mQueue = CustomVolleyRequestQueue.getInstance(getApplicationContext())
.getRequestQueue();
String url = "http://192.168.5.101:8080/FremdenfuehrerWeb/webresources/author";
final Author registeredAuthor = new Author(eMail, password);
Gson gson = new Gson();
String json = gson.toJson(registeredAuthor);
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(json.substring(json.indexOf("{"), json.lastIndexOf("}") + 1));
} catch (JSONException e) {
e.printStackTrace();
}
CustomJSONObjectRequest customJSONObjectRequest =
new CustomJSONObjectRequest(
Request.Method.POST,
url,
jsonObject,
RegisterActivity.this,
RegisterActivity.this
);
mQueue.add(customJSONObjectRequest);
内容类型为application/json
。
请帮忙!谢谢!
修改 我通过从Android Volley更改为Retrofit Framework来解决问题