2009-11-05 30 views
0

我的問題,以創建新的「騎」非常相似,Java: HTTP Post to create new "Product" in a Ruby on Rails application的Java:HTTP Post在一個Ruby on Rails應用程序

我有,我想「發帖」,看起來像這樣

表格形式
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> 

我的Java代碼看起來像這樣

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(); 
      } 
     } 

下面是RoR的控制檯輸出

Processing RidesController#create (for 172.16.60.1 at 2009-11-04 22:22:52) [POST] 
Parameters: {"_json"=>"totake=Ride+to+Vail&togive=%2420"} 
Ride Columns (0.6ms) SHOW FIELDS FROM `rides` 
SQL (0.1ms) BEGIN 
Ride Create (0.3ms) INSERT INTO `rides` (`isoffer`, `togive`, `howlong`, `updated_at`,  `totake`, `created_at`) 
VALUES(NULL, NULL, 0, '2009-11-05 05:22:52', NULL, '2009-11-05 05:22:52') 
SQL (0.0ms) COMMIT 
Redirected to http://172.16.60.129:3000/rides 
Completed in 9ms (DB: 1) | 302 Found [http://172.16.60.129/rides] 

我想我真正的問題是我應該如何解決Java中的RoR變量?

+0

我不知道你的意思回報率的變量,但是當每一件事情是發生在HTTP POST或GET那麼所有這些值將在HTTP提供迴應權? – shivaspk

+0

我的意思是RoR變量是RoR生成的html表單,它位於我的問題的頂部。如何在java中的這些字段中插入值? –

回答

2

在您的html輸入標籤中使用名稱而不是id。在你的例子中,他們是「乘坐[原諒]」和「乘坐[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); 

從回報率控制檯:

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"}} 
+0

我嘗試了這一點,當我做到這一點時,我得到了與我在帖子中發佈的RoR終端響應相同的值,其中包含null原諒值和totake值。有點奇怪。任何其他想法? –

+0

修改答案。將RoR控制檯與輸出進行比較: 參數:{「_json」=>「totake = Ride + to + Vail&togive =%2420」} 注意:我不必在我的示例中將內容類型設置爲application/json 。 – marklai

+0

不幸的是,這個答案看起來像它使用HttpClient的舊3.1,如果有人想用4.1運行它,這是不可能的。我試圖降級到3.1,但它導致與AuthenticityToken衝突,我收到一個ActionController :: InvalidAuthenticityToken。我相信當內容類型設置爲json時,它會處理這個錯誤。 –