2013-06-12 43 views
0

我想从json发送数据到等价的php格式输出使用json.put();使用httpclient发送Json objet到drupal Android

Array 
    (
     [node] => Array 
      (
       [name] => admin 
       [type] => mobile_content 
       [language] => und 
       [title] => re resss [email protected] 
       [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] => [email protected] 
            ) 

          ) 

        ) 

       [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到服务器的正确方法。

+0

给出一些更多异常日志行。它似乎在Android方面异常,但不是在服务器上,但需要更多的异常日志行。 –

回答

0

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

{"type":"mobile_content"} 

和不存在拼写错误或大写字母的JSON,因为它是区分大小写。对标题也一样。

相关问题