简体   繁体   English

如何将JSONObject发送到REST服务?

[英]How to send a JSONObject to a REST service?

Retrieving data from the REST Server works well, but if I want to post an object it doesn't work: 从REST服务器检索数据效果很好,但如果我想发布一个对象则不起作用:

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. 我总是得到400错误代码。 Does anybody know whats wrong? 有谁知道什么是错的?

I have working C# code, but I can't convert: 我有C#代码,但我无法转换:

 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"); 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 它有一个REST 客户端库 ,使这些事情更容易,更易读

Your C# is different than your Java, and not just in syntax. 您的C#与Java不同,而不仅仅是语法。

Your C# sends an application/json entity to the server via HTTP POST. 您的C#通过HTTP POST将application/json实体发送到服务器。 I'll leave it up to HTTP purists as to whether that's appropriate use of POST (vs. PUT). 我会告诉HTTP纯粹主义者是否适当使用POST(与PUT相比)。

Your Java creates a form, with a field of jsonString (whose value is the JSON), and sends an application/x-www-form-urlencoded entity to the server containing that form. 您的Java创建一个表单,其字段为jsonString (其值为JSON),并将application/x-www-form-urlencoded实体发送到包含该表单的服务器。

I would go right to the server err_log or equivelant error log. 我会直接到服务器err_log或equivelant错误日志。 The server knows why it rejected your request. 服务器知道它拒绝您的请求的原因。 If you don't have access, set up your own test server and duplicate the issue there so you can review the logs =) 如果您没有访问权限,请设置您自己的测试服务器并在那里复制问题,以便查看日志=)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM