我正在使用Android的loopj的AsyncHttpClient,以便我可以与我创建的宁静的Web应用程序进行交互。我使用Postman测试了一个POST请求,它工作正常。为Rest客户端设置Json内容类型
然而,在Android中,我努力做一个POST请求但作为内容型总是被设置为text/html ..
RequestParams params = new RequestParams();
params.setUseJsonStreamer(true);
params.put("email", "[email protected]");
StringEntity se = null;
try {
se = new StringEntity(params.toString());
se.setContentType("application/json");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Header headers[] = {};
if(getActivity() != null){
RestClient.postWithContentType(getActivity(), "contacts", se, "application/json", new AsyncHttpResponseHandler() {
//onSuccess and onFailure methods ommitted
});
它不断失败,而我得到的logcat此消息: 传递的contentType将被忽略,因为HttpEntity设置内容类型。
所以,我试图改变这一状况,
public static void postWithContentType(Context context,String url,StringEntity s,String contentType, AsyncHttpResponseHandler responseHandler){
s.setContentType("application/json");
client.post(context, getAbsoluteUrl(url), s, contentType, responseHandler);
}
但我仍然得到同样的消息,这真是令人沮丧,我一直在试图弄明白了好半天! 如果任何人有任何关于如何设置内容类型的想法 - 将不胜感激,谢谢!
RestClient究竟是什么?你还没有说过这些。也许它的实施会弄乱某些东西? – 0101100101 2014-09-26 14:49:32
你解决了这个问题吗? – playmaker420 2014-11-10 11:36:34