【发布时间】:2010-10-16 07:24:20
【问题描述】:
在 android 上使用 Apache HttpClient,如何使用 HttpPost 将数据发送到 RESTfull Ruby on Rails 应用程序。
这是我的控制器:
# 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 代码。我应该在 URL 名称中传递数据,还是必须将其设置在其他地方? (也许是httppost.setEntity?)最终我将使用JSON,但现在我只想得到它,这样我就可以在Rails 中调用“create”方法。 Rails 正在获取 POST,但从未在“create”方法中执行任何代码
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.0.100:3000/products/new");
HttpResponse response = httpclient.execute(httppost);
我很困惑,如果有人能指出我正确的方向,我将不胜感激。
【问题讨论】:
标签: java ruby-on-rails android http