Java:HTTP Post在Ruby on Rails应用程序中创建新的“Product”

时间:2022-05-23 01:51:37

Using the Apache HttpClient on android, how do I send data to a RESTfull Ruby on Rails app using HttpPost.

在Android上使用Apache HttpClient,如何使用HttpPost将数据发送到RESTfull Ruby on Rails应用程序。

This is my Controller:

这是我的控制器:

# 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

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

这是我的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);

I am quite stuck and would appreciate if someone could point me in the right direction.

我很困惑,如果有人能指出我正确的方向,我会很感激。

3 个解决方案

#1


I added the following to my POST request and it worked like a charm.

我在POST请求中添加了以下内容,它就像一个魅力。

httppost.addHeader("Content-Type","application/json");

#2


Rails is not executing any code in the Create method because you are making a request to the "New" method.

Rails没有在Create方法中执行任何代码,因为您正在向“New”方法发出请求。

Typically the New method is used to render an HTML form that the user then submits to the create method. For a web service this method is not needed.

通常,New方法用于呈现用户随后提交给create方法的HTML表单。对于Web服务,不需要此方法。

Use the url http://192.168.0.100:3000/products (without /new) instead. By default, rails will route this request to the Create method by looking at the request type and seeing that it is a POST request.

请使用网址http://192.168.0.100:3000/products(不带/新)。默认情况下,rails会通过查看请求类型并查看它是POST请求,将此请求路由到Create方法。

This assumes you have correctly set up Products to be a RESTful resource in your routes. Otherwise you will need to use http://192.168.0.100:3000/products/create. Here is the API doc for RESTful resources and routes: http://api.rubyonrails.org/classes/ActionController/Resources.html

这假设您已正确设置Products作为路由中的RESTful资源。否则,您将需要使用http://192.168.0.100:3000/products/create。以下是RESTful资源和路由的API文档:http://api.rubyonrails.org/classes/ActionController/Resources.html

#3


To submit data in a POST request, you need to call httppost.setRequestBody(). This method takes an array of NameValuePairs with your data - consult the HTTP Client documentation for the correct syntax.

要在POST请求中提交数据,您需要调用httppost.setRequestBody()。此方法将NameValuePairs数组与您的数据一起使用 - 请参阅HTTP Client文档以获取正确的语法。

#1


I added the following to my POST request and it worked like a charm.

我在POST请求中添加了以下内容,它就像一个魅力。

httppost.addHeader("Content-Type","application/json");

#2


Rails is not executing any code in the Create method because you are making a request to the "New" method.

Rails没有在Create方法中执行任何代码,因为您正在向“New”方法发出请求。

Typically the New method is used to render an HTML form that the user then submits to the create method. For a web service this method is not needed.

通常,New方法用于呈现用户随后提交给create方法的HTML表单。对于Web服务,不需要此方法。

Use the url http://192.168.0.100:3000/products (without /new) instead. By default, rails will route this request to the Create method by looking at the request type and seeing that it is a POST request.

请使用网址http://192.168.0.100:3000/products(不带/新)。默认情况下,rails会通过查看请求类型并查看它是POST请求,将此请求路由到Create方法。

This assumes you have correctly set up Products to be a RESTful resource in your routes. Otherwise you will need to use http://192.168.0.100:3000/products/create. Here is the API doc for RESTful resources and routes: http://api.rubyonrails.org/classes/ActionController/Resources.html

这假设您已正确设置Products作为路由中的RESTful资源。否则,您将需要使用http://192.168.0.100:3000/products/create。以下是RESTful资源和路由的API文档:http://api.rubyonrails.org/classes/ActionController/Resources.html

#3


To submit data in a POST request, you need to call httppost.setRequestBody(). This method takes an array of NameValuePairs with your data - consult the HTTP Client documentation for the correct syntax.

要在POST请求中提交数据,您需要调用httppost.setRequestBody()。此方法将NameValuePairs数组与您的数据一起使用 - 请参阅HTTP Client文档以获取正确的语法。