Java的:HTTP发布创建新的"产品"在Ruby on Rails应用 [英] Java: HTTP Post to create new "Product" in a Ruby on Rails application
问题描述
使用Apache HttpClient的android上,我怎么使用HttpPost送on Rails应用程序数据到一个REST风格的Ruby。
Using the Apache HttpClient on android, how do I send data to a RESTfull Ruby on Rails app using HttpPost.
这是我的控制器:
# POST /products
def create
@product = Product.new(params[:product])
respond_to do |format|
if @product.save
flash[:notice] = 'Product was successfully created.'
format.html { redirect_to(@product) }
format.xml { render :xml => @product, :status => :created, :location => @product }
else
format.html { render :action => "new" }
format.xml { render :xml => @product.errors, :status => :unprocessable_entity }
end
end
end
这是我的Java code。我应该传递数据的URL名称,或者我需要设置在别的地方? (httpost.setEntity吧?)最后,我将使用JSON但现在我只是想获得它,所以我实际上可以调用Rails的创造的方法。 Rails是获得职位,但从来没有在创造的方法执行任何code
And this is my Java Code. Should I be passing the data in the URL name, or do I have to set it somewhere else? (httpost.setEntity perhaps?) Eventually I will be using JSON but for now I just want to get it so I can actually call the "create" method in Rails. Rails is getting the POST but never executes any code in the "create" method
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.0.100:3000/products/new");
HttpResponse response = httpclient.execute(httppost);
我很卡住,将AP preciate如果有人可以点我在正确的方向。
I am quite stuck and would appreciate if someone could point me in the right direction.
推荐答案
我增加了以下我的POST请求和它的工作就像一个魅力。
I added the following to my POST request and it worked like a charm.
httppost.addHeader("Content-Type","application/json");
这篇关于Java的:HTTP发布创建新的"产品"在Ruby on Rails应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!