bro*_*31k 5 java android ruby-on-rails http
在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
Run Code Online (Sandbox Code Playgroud)
这是我的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);
Run Code Online (Sandbox Code Playgroud)
我很困惑,如果有人能指出我正确的方向,我将不胜感激。
我将以下内容添加到我的 POST 请求中,效果非常好。
httppost.addHeader("Content-Type","application/json");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2862 次 |
| 最近记录: |