0

我正在尝试使用 json.put() 将数据从 json 发送到输出下方的等效 php 格式;

Array
    (
        [node] => Array
            (
                [name] => admin
                [type] => mobile_content
                [language] => und
                [title] => re resss re@fffd.com
                [field_one] => Array
                    (
                        [und] => Array
                            (
                                [0] => Array
                                    (
                                        [value] => re
                                    )

                            )

                    )

                [field_prenom] => Array
                    (
                        [und] => Array
                            (
                                [0] => Array
                                    (
                                        [value] => resss
                                    )

                            )

                    )

                [field_addr] => Array
                    (
                        [und] => Array
                            (
                                [0] => Array
                                    (
                                        [value] => re@fffd.com
                                    )

                            )

                    )

                [field_email] => Array
                    (
                        [und] => Array
                            (
                                [0] => Array
                                    (
                                        [value] => 01/01/1913
                                    )

                            )

                    )

                [field_collectif] => Array
                    (
                        [und] => Array
                            (
                                [0] => Array
                                    (
                                        [value] => 1
                                    )

                            )

                    )

                [field_bach] => Array
                    (
                        [und] => Array
                            (
                                [0] => Array
                                    (
                                        [value] => 0
                                    )

                            )

                    )

                [field_mc_rec] => Array
                    (
                        [und] => Array
                            (
                                [0] => Array
                                    (
                                        [value] => 1
                                    )

                            )

                    )

                [field_photo] => Array
                    (
                        [und] => Array
                            (
                                [0] => Array
                                    (
                                        [fid] => 1778
                                    )

                            )

                    )

            )

    )

我的问题是我无法正确获取格式。

i created my json object



        try {


            JSONObject json = new JSONObject();

然后把节点使用

json.put("node[type]","type"); 

然后最后将json输出发送到

DefaultHttpClient httpClient = new DefaultHttpClient();
    ResponseHandler<String> resonseHandler = new BasicResponseHandler();

    HttpPost postMethod = new HttpPost(path);

    String authorizationString = "Basic " + Base64.encodeToString(
            ("uname" + ":" + "pword").getBytes(),
            Base64.NO_WRAP);

    postMethod.setHeader("Authorization", authorizationString);
    postMethod.setHeader("Content-Type","application/x-www-form-urlencoded");       
    postMethod.setHeader("Accept", "application/json");

    postMethod.setEntity(new ByteArrayEntity(params.toString().getBytes("UTF8")));

    String response = httpClient.execute(postMethod,resonseHandler);

    if (response.equals("[]")) //empty
        return null;
    else
    {
        System.out.println("response :" + response);
        JSONObject jObject=new JSONObject(response);

        return jObject;
    }

我的错误日志显示

org.apache.http.client.HttpResponseException: Not Acceptable: Node type is required

但在服务器端我没有显示日志。表示未发送 json 对象。这很奇怪。请注意,我正在发送到 drupal。请帮忙。这是将文本格式 json 发送到服务器的正确方法吗?

4

2 回答 2

0

在我的情况下,我做

StringEntity se = new StringEntity(postObject
                                .toString(), HTTP.UTF_8);
                        se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,
                                "application/json"));
                        request.setEntity(se);
                        HttpResponse response = httpclient.execute(request);

然后读取响应实体。但我编写了自己的服务器端 php API。

还检查类似的问题:https ://stackoverflow.com/questions/17031090/how-to-prevent-http-406-status-code-error-from-server

于 2013-06-12T18:07:36.797 回答
0

几天前我遇到了类似的问题。创建节点时,类型和标题是要创建的节点的必填字段。您说的错误是未提供“类型”。我会打印出您尝试发布的 JSON,并确保类型字段看起来像

{"type":"mobile_content"}

并且没有拼写错误或大写字母,因为它区分大小写。对标题也做同样的事情。

于 2013-06-12T18:07:52.137 回答