简体   繁体   English

Ruby on Rails通过Android应用程序对POST的响应

[英]Ruby on Rails response to POST via Android app

I have written a small RoR project with devise and scaffolding. 我写了一个带有设计和脚手架的小型RoR项目。 When registering new user via my desktop I get different result from when registering new user from my android app (using JSON). 通过桌面注册新用户时,与从Android应用程序注册新用户(使用JSON)时,得到的结果不同。

Here is part of the source from the desktop: 这是桌面来源的一部分:

<body>
<p class="notice">Welcome! You have signed up successfully.</p>
<p class="alert"></p>
.
.

And here is the response I get in my android app: 这是我在Android应用程序中得到的响应:

<body>
    <p class="notice"></p>
    <p class="alert"></p>
    .
    .

Why don't I get the notice? 为什么我没有收到通知? I know the app is working and the user has been created. 我知道该应用程序正在运行,并且已经创建了用户。

Edit: Maybe there is a way to configure Devise how to respond when registering via JSON? 编辑:也许有一种方法可以配置Devise通过JSON注册时如何响应? I don't need the html.. All I need is a confirmation.. 我不需要html ..我需要的只是一个确认..

Edit2: So now I added httppost.setHeader("Accept", "application/json"); Edit2:所以现在我添加了httppost.setHeader("Accept", "application/json"); The user is created but my response is completely empty now. 用户已创建,但我的响应现在完全空白。 I need to figure how to make Devise create a JSON (custom?) response.. 我需要弄清楚如何使Devise创建JSON(自定义?)响应。

Apparently, all I needed was to add this: 显然,我需要添加以下内容:

respond_to :html, :json

to gems/[Devise folder]/app/controllers/devise/registration_controller gems/[Devise folder]/app/controllers/devise/registration_controller

So it will be simpler just to add this line to devise_controller, so we will have JSON response in all the controllers. 因此,将这一行添加到devise_controller中会更加简单,因此我们将在所有控制器中获得JSON响应。

# All Devise controllers are inherited from here.
class DeviseController < Devise.parent_controller.constantize
  include Devise::Controllers::ScopedViews
  respond_to :html, :json
.
.
.

In the android app, we need to remember: 在android应用中,我们需要记住:

httppost.setHeader("content-type", "application/json; charset= utf-8");
httppost.setHeader("Accept", "application/json");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM