如何发送的JSONObject到REST服务? [英] How to send a JSONObject to a REST service?

查看:166
本文介绍了如何发送的JSONObject到REST服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从REST服务器检索数据效果很好,但如果我要发布一个对象这是行不通的:

 公共静态无效postJSONObject(INT store_type,FavoriteItem喜爱,字符串令牌,字符串对象名){
        字符串URL =;

        开关(store_type){
            案例STORE_PROJECT:
                URL = URL_STORE_PROJECT_PART1 +令牌+ URL_STORE_PROJECT_PART2;
                //数据= favorite.getAsJSONObject();
            打破;
        }

        HttpClient的HttpClient的=新DefaultHttpClient();
        HttpPost的PostMethod =新HttpPost(URL);

        尝试 {
            HttpEntity实体=新StringEntity({\ID \:0,\名称\:\我的Projekt10 \});

            postMethod.setEntity(实体);

            HTT presponse响应= httpClient.execute(后方法);
            Log.i(JSONStore,发布采购信息,以URL:+网址);
            的System.out.println(状态code:+ response.getStatusLine()的getStatus code());

        }赶上(ClientProtocolException E){
 

我总是得到一个400错误code。有谁知道什么是错了吗?

我已经运行的C#code,但我不能转换:

  System.Net.WebRequest WR = System.Net.HttpWebRequest.Create(HTTP://本地主机:51273 / WSUser.svc / pak3omxtEuLrzHSUSbQP /项目);
            wr.Method =POST;
            字符串数据={\ID \:1,\名称\:\我的Projekt的\};

            byte []的D​​ = UTF8Encoding.UTF8.GetBytes(数据);
            wr.ContentLength = d.Length;
            wr.ContentType =应用/ JSON;

             wr.GetRequestStream()写(D,0,d.Length)。
            System.Net.WebResponse wresp = wr.GetResponse();
            就是System.IO.StreamReader SR =新就是System.IO.StreamReader(wresp.GetResponseStream());
            串线= sr.ReadToEnd();
 

解决方案

尝试设置的内容类型头:

  

postMethod.addRequestHeader(内容类型,应用/ JSON);

顺便说一句,我强烈建议新泽西。它有一个REST 客户端库这使得这些类的东西更容易和更具可读性

Retrieving data from the REST Server works well, but if I want to post an object it doesn't work:

public static void postJSONObject(int store_type, FavoriteItem favorite, String token, String objectName) {
        String url = "";

        switch(store_type) {
            case STORE_PROJECT:
                url = URL_STORE_PROJECT_PART1 + token + URL_STORE_PROJECT_PART2; 
                //data = favorite.getAsJSONObject();
            break;
        }

        HttpClient httpClient = new DefaultHttpClient();
        HttpPost postMethod = new HttpPost(url);

        try {   
            HttpEntity entity = new StringEntity("{\"ID\":0,\"Name\":\"Mein Projekt10\"}");

            postMethod.setEntity(entity);

            HttpResponse response = httpClient.execute(postMethod);
            Log.i("JSONStore", "Post request, to URL: " + url);
            System.out.println("Status code: " + response.getStatusLine().getStatusCode());

        } catch (ClientProtocolException e) {

I always get a 400 Error Code. Does anybody know whats wrong?

I have working C# code, but I can't convert:

 System.Net.WebRequest wr = System.Net.HttpWebRequest.Create("http://localhost:51273/WSUser.svc/pak3omxtEuLrzHSUSbQP/project");
            wr.Method = "POST";
            string data = "{\"ID\":1,\"Name\":\"Mein Projekt\"}";

            byte [] d = UTF8Encoding.UTF8.GetBytes(data);
            wr.ContentLength = d.Length;
            wr.ContentType = "application/json";

             wr.GetRequestStream().Write(d, 0, d.Length);
            System.Net.WebResponse wresp = wr.GetResponse();
            System.IO.StreamReader sr = new System.IO.StreamReader(wresp.GetResponseStream());
            string line = sr.ReadToEnd();

解决方案

Try setting the content type header:

postMethod.addRequestHeader("Content-Type", "application/json");

Btw, I strongly recommend Jersey. It has a REST client library which makes these kind of things much easier and more readable

这篇关于如何发送的JSONObject到REST服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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