Java:HTTP Post创建新的“;骑乘”;在RubyonRails应用程序中

Java:HTTP Post创建新的“;骑乘”;在RubyonRails应用程序中,java,ruby-on-rails,httpclient,Java,Ruby On Rails,Httpclient,我的问题很类似于 我有一个表单,我想“发布”到一个如下所示的表单 Offer:<br /> <input id="ride_togive" name="ride[togive]" size="30" type="text" /> </p> <p> Request for Offer:<br /> <input id="ride_totake" name="ride[totake]" size="30" type="tex

我的问题很类似于

我有一个表单,我想“发布”到一个如下所示的表单

Offer:<br /> 
<input id="ride_togive" name="ride[togive]" size="30" type="text" /> 
</p> 
<p> 
Request for Offer:<br /> 
<input id="ride_totake" name="ride[totake]" size="30" type="text" /> 
</p> 
DefaultHttpClient client = new DefaultHttpClient();

         HttpPost post = new HttpPost("http://172.16.60.129:3000/rides");

     // Configure the form parameters
        List <NameValuePair> nvps = new ArrayList <NameValuePair>();
        nvps.add(new BasicNameValuePair("ride_totake", "Ride to Vail"));
        nvps.add(new BasicNameValuePair("ride_togive", "$20"));

        post.addHeader("Content-Type","application/json");

        try {
            post.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        HttpResponse response = null;
        try {
            response = client.execute(post);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        HttpEntity entity = response.getEntity();
        System.out.println("form get: " + response.getStatusLine());
        if (entity != null) {
            try {
                entity.consumeContent();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

我想我真正的问题是如何在Java中处理RoR变量?

在html输入标记中使用名称而不是id。在您的示例中,它们是“ride[togive]”和“ride[totake]”

在我的RoR项目上工作的示例Java代码。使用HttpClient 3.1

    PostMethod post = new PostMethod("http://localhost:3000/projects");
NameValuePair[] data = {
  new NameValuePair("project[name]", "from java"),
  new NameValuePair("project[life_cycle_id]", "5")
};
post.setRequestBody(data);
// execute method and handle any error responses.

new HttpClient().executeMethod(post);
从RoR控制台:

Processing ProjectsController#create (for 127.0.0.1 at 2009-11-04 22:07:39) [POS
T]
  Parameters: {"project"=>{"name"=>"from java", "life_cycle_id"=>"5"}}

我不知道你说的RoR变量是什么意思,但是当每件事都发生在HTTP POST或GET上时,所有这些值都将在HTTP响应中可用,对吗?我所说的RoR变量是指由RoR生成的html表单,它位于我问题的顶部。如何在java中向这些字段中插入值?我尝试了这个方法,当我得到相同的RoR终端响应时,我在帖子中发布了null togive和totake值。很奇怪。还有其他想法吗?修改答案。将RoR控制台与您的输出进行比较:参数:{“\u json”=>“totake=Ride+to+Vail&togive=%2420”}注意:在我的示例中,我不必将内容类型设置为application/json。不幸的是,这个答案看起来像是使用了HttpClient的旧3.1版本,如果要用4.1运行它,这是不可能的。我尝试降级到3.1,但它导致与AuthenticityToken冲突,我收到一个ActionController::InvalidAuthenticityToken。我相信,当内容类型设置为json时,它将处理此错误。为了确认,我在RoR端禁用了AuthenticityToken后,确实使此代码正常工作。如果有人知道如何使用AuthenticityToken,请告诉我-拍打
Processing ProjectsController#create (for 127.0.0.1 at 2009-11-04 22:07:39) [POS
T]
  Parameters: {"project"=>{"name"=>"from java", "life_cycle_id"=>"5"}}