设置的Json内容类型的REST客户端 [英] Setting Json content-type for Rest client

查看:329
本文介绍了设置的Json内容类型的REST客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是循环J的AsyncHttpClient为Android,这样我可以与我创建了一个基于REST的Web应用程序进行交互。我已经测试使用邮差POST请求,它工作正常。

I'm using loopj's AsyncHttpClient for Android so that I can interact with a restful web application which I have created. I have tested a POST request using Postman and it works fine.

不过,在Android中,我努力做一个POST请求但是作为内容类型总是设置为text / html。

However, in Android I'm struggling to do a post request however as the content-type is always set as 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

    });

它不断失败,我得到这个消息在logcat的: 通过的contentType会被忽略,因为HttpEntity设置的内容类型。

It keeps failing, and I get this message in logcat: Passed contentType will be ignored because HttpEntity sets content type.

所以,我试图改变这一点,

So, I attempted to change this,

 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); 
  }

不过,我仍然得到同样的消息,这真是令人沮丧,我一直在试图弄明白了好半天! 如果任何人有关于如何设置内容类型的任何想法 - !这将是pciated多少AP $ P $,谢谢

However I still get the same message, it's really frustrating, and I've been trying to figure it out for ages! If anyone has any idea about how to set the content type - it will be much appreciated,thanks!

推荐答案

我有同样的问题。

我不明白这会影响它,因为它确实有效天前。我只是再次手动添加标题和它的作品。

I cannot understand which affects it because it really works days ago. I just add header again manually and it works.

只需添加 RestClient.addHeader(内容类型,应用/ JSON); 上方的 RestClient.postWithContentType(... ) code。

Just add RestClient.addHeader("Content-Type", "application/json"); above your RestClient.postWithContentType(...) code.

这篇关于设置的Json内容类型的REST客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆