1

在这里,我想post idea从. 为此,我没有确切的想法,但到目前为止我已经尝试过。drupalandroid

我尝试了帖子titlebody。我正在将标题和正文转换为json格式并传递给服务器。我尝试发布individual title and bodycombine title & body

添加个人标题和正文时:

 List<NameValuePair> params = new ArrayList<NameValuePair>();


          params.add(new BasicNameValuePair("type","page"));
          params.add(new BasicNameValuePair("title",title));
          params.add(new BasicNameValuePair("body",jsonnode.toString()));

                         // jsonnode value is {"und":[{"value":"body value"}]}

         System.out.println("value of the params =============> "+params);

给我吗status code 401

I/System.out(  524): @@@@@@2    jsonnode=======>{"und":[{"value":"body value"}]}
I/System.out(  524): value of the combine node=======>{"type":"article","body":{"und":[{"value":"body value"}]},"title":"title"}
I/System.out(  524): value of the params =============> [type=page, title=title, body={"und":[{"value":"body value"}]}]
W/DefaultRequestDirector(  524): Authentication error: Unable to respond to any of these challenges: {}
I/System.out(  524): =========> statusCode post idea=====> 401

结合标题和正文:

 List<NameValuePair> params = new ArrayList<NameValuePair>();

          params.add(new BasicNameValuePair("node",json.toString()));   
                        //  json value is  {"type":"article","body":{"und":[{"value":"body"}]},"title":"title"}

            System.out.println("value of the params =============> "+params);

给我吗status code 406

I/System.out(  571): @@@@@@2    jsonnode=======>{"und":[{"value":"body"}]}
I/System.out(  571): value of the combine node=======>{"type":"article","body":{"und":[{"value":"body"}]},"title":"title"}
I/System.out(  571): value of the params =============> [node={"type":"article","body":{"und":[{"value":"body"}]},"title":"title"}]
I/System.out(  571): =========> statusCode post idea=====> 406

连接:

public static String makeWebForPostIdea(String url, String title,String body)
    {
        JSONStringer jsonobject = null;
        JSONObject json = null;
        JSONObject jsonnode = null;

        DefaultHttpClient client = new DefaultHttpClient();

        HttpPost post = new HttpPost(url);

        try {
            JSONObject jsonvalue = new JSONObject();
            jsonvalue.put("value", body.toString());

            JSONArray array = new JSONArray();
            array.put(jsonvalue);

            jsonnode = new JSONObject();
            jsonnode.put("und", array);

            System.out.println("@@@@@@2    jsonnode=======>"+jsonnode.toString());


        } catch (JSONException e3) {
            // TODO Auto-generated catch block
            e3.printStackTrace();
        }

        try {
               //jsonobject = new JSONStringer().object().key("und").array().object().key("value").value(body).endObject().endArray().endObject();

               json = new JSONObject();
               json.put("title",title);
               json.put("body", jsonnode);
               json.put("type","article");

            System.out.println("value of the combine node=======>"+json.toString());   
            //System.out.println("=============>"+jsonobject);

        } catch (JSONException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }

         List<NameValuePair> params = new ArrayList<NameValuePair>();

          params.add(new BasicNameValuePair("node",json.toString()));
           // params.add(new BasicNameValuePair("type","page"));
            //params.add(new BasicNameValuePair("title",title));
            //params.add(new BasicNameValuePair("body",jsonnode.toString()));

            System.out.println("value of the params =============> "+params);

        UrlEncodedFormEntity formEntity = null;
        try {
                formEntity = new UrlEncodedFormEntity(params);
        } catch (UnsupportedEncodingException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        post.setEntity(formEntity);

        try {

            HttpResponse response = client.execute(post);

            int statusCode = response.getStatusLine().getStatusCode();
            System.out.println("=========> statusCode post idea=====> "+statusCode);    
            if (statusCode == HttpStatus.SC_OK)
            {
                HttpEntity entity = response.getEntity();
                InputStream is = entity.getContent();
                return iStream_to_String(is);
            }
            else
            {
                return "Hello This is status ==> :"+String.valueOf(statusCode);
            }
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }

     public static String iStream_to_String(InputStream is1) {
            BufferedReader rd = new BufferedReader(new InputStreamReader(is1), 4096);
            String line;
            StringBuilder sb = new StringBuilder();
            try {
                while ((line = rd.readLine()) != null) {
                    sb.append(line);
                }
                rd.close();

            } catch (IOException e) {
                // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String contentOfMyInputStream = sb.toString();
        return contentOfMyInputStream;
    }

}
4

1 回答 1

0

我不知道您的 Drupal 站点上有什么样的自定义代码,但不可能发布到 Web 界面之外的标准 Drupal 站点——这是一种安全措施。

您的最佳选择是:

a:在 Drupal 站点上创建您自己的自定义 API 模块并发布到该模块(确保添加某种安全措施以确保只有您的应用可以发布到它)

b:使用 Android 的 WebView 对象直接显示 Drupal 页面 - 让用户发布到该页面。

于 2012-08-10T03:49:36.277 回答