我正在为 Android 使用 loopj 的 AsyncHttpClient,以便我可以与我创建的宁静的 Web 应用程序进行交互。我已经使用 Postman 测试了一个 POST 请求,它工作正常。
但是,在 Android 中,我正在努力执行发布请求,因为内容类型始终设置为 text/html ..
RequestParams params = new RequestParams();
params.setUseJsonStreamer(true);
params.put("email", "android@tester.com");
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
});
Run Code Online (Sandbox Code Playgroud)
它一直失败,我在 logcat 中收到此消息:传递的内容类型将被忽略,因为 HttpEntity 设置了内容类型。
所以,我试图改变这一点,
public static void postWithContentType(Context context,String url,StringEntity …Run Code Online (Sandbox Code Playgroud)