Java:通过HTTP Post在Ruby on Rails应用程序中创建新的“产品”


问题内容

在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名称中传递数据,还是必须将其设置在其他位置?(也许是httpost.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");