在 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);
我很困惑,如果有人能指出我正确的方向,我将不胜感激。
我将以下内容添加到我的 POST 请求中,效果非常好。
httppost.addHeader("Content-Type","application/json");
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)