【问题标题】:Ruby on Rails default to POST when accessed with Android Studio使用 Android Studio 访问时,Ruby on Rails 默认为 POST
【发布时间】:2016-09-29 08:24:13
【问题描述】:

我有一些使用 android studio 的经验,但对 ruby​​ on rails 还很陌生,并且一直在尝试创建一个安静的应用程序。我生成了一个名为 Employer 的脚手架,因此它具有默认路由,

employers GET    /employers(.:format)    employers#index
          POST   /employers(.:format)    employers#create

并且一直在尝试通过在 android studio 中解析 JSON 来读取雇主列表:

ProgressDialog pdLoading = new ProgressDialog(MainActivity.this);
    HttpURLConnection conn;
    URL url = null;
    String response = "";
    TextView errdisplay = (TextView)findViewById(R.id.fishytext);

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        //this method will be running on UI thread
        pdLoading.setMessage("\tLoading...");
        pdLoading.setCancelable(false);
        pdLoading.show();

    }

    @Override
    protected String doInBackground(String... params) {
        try {

            // Enter URL address where your json file resides
            url = new URL("MYIP.../employers.json");


        } catch (MalformedURLException e) {

            // TODO Auto-generated catch block
            e.printStackTrace();
            return e.toString();
        }
        try {

            // Setup HttpURLConnection class to send and receive data from php and mysql
            conn = (HttpURLConnection) url.openConnection();

            conn.setReadTimeout(READ_TIMEOUT);
            conn.setConnectTimeout(CONNECTION_TIMEOUT);
            conn.setRequestMethod("GET");

            // setDoOutput to true as we recieve data from json file
            conn.setDoOutput(true);

        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
            return e1.toString();
        }

        try {

            int response_code = conn.getResponseCode();

            // Check if successful connection made
            if (response_code == HttpURLConnection.HTTP_OK) {


                // Read data sent from server
                InputStream input = conn.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(input));
                StringBuilder result = new StringBuilder();
                String line;

                while ((line = reader.readLine()) != null) {
                    result.append(line);
                    response+= line;
                }
                return (result.toString());

            } else {
                return ("unsuccessful");
            }

        } catch (IOException e) {
            e.printStackTrace();
            return e.toString();
        } finally {
            conn.disconnect();
        }
    }

我的问题是,尽管将我的 RequestMethod 更改为 GET,但它在 rails 中默认为 POST,这让我很困惑。

Started POST "/employers.json"

我知道这是一个路由问题,并且已经设法通过强制更改路由来使其工作(如果你可以这样称呼它),如下所示,但我意识到这将影响我未来对 REST 的尝试。

post 'employers' => 'employers#index'

如果这不是正确的格式,我深表歉意,但我将非常感谢有关如何修复我的路线或如何访问 JSON url 的任何建议。感谢您的宝贵时间。

【问题讨论】:

  • 请求来自 Android Studio 的 POST。你能确认 Java 代码发出的请求类型是 POST/GET 吗?
  • 我可以确认请求方法是GET,即使我没有在上面的代码中设置GET似乎是android studio中的默认值。谢谢。
  • 你能发布routes.rb中为雇主指定的路线吗?用于测试请求是否以 get 的形式出现。只要有get "/employers", to: "employers#index"。上一行将确保没有定义 POST 路由,如果您仍然收到 POST,则意味着您必须在工作室中更改代码。
  • 请求似乎仍以 POST 的形式传入。这是我的路线employers GET /employers(.:format) employers#index POST /employers(.:format) employers#create new_employer GET /employers/new(.:format) employers#new edit_employer GET /employers/:id/edit(.:format) employers#edit employer GET /employers/:id(.:format) employers#show
  • Rails 无法更改传入请求的请求类型。它只是将传入请求与定义的路由匹配。所以这里的请求是作为 POST 来的。要检查 Rails 是否理解获取“/employers”,到:“employers/index”,您是否还可以在 chrome 或 firefox 中安装邮递员扩展并尝试发出获取请求:localhost:3000/employers.json,请求类型为 GET。如果这正常工作,那么 Rails 路由是正确的。

标签: ruby-on-rails json android-studio


【解决方案1】:

非常感谢用户 sahil 为我指明了正确的方向。目前,除了 POST 之外,我还没有设法访问 Rails,因此对于处于类似情况的任何人,我使用的解决方法是更改​​路由以修改 url。

post 'employers/display' => 'employers#index'
post 'employers/newindex' => 'employers#create'

所以我需要访问的 url 将是“/employers/display.json”(用于 GET)和“/employers/newindex.json”(用于 POST),根据我在employees_controller 中的定义方式。这不是一个很好的答案,但当我知道更多时会再次更新。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-25
    • 1970-01-01
    • 2011-06-16
    • 1970-01-01
    • 1970-01-01
    • 2017-06-02
    • 2017-04-08
    相关资源
    最近更新 更多