从Android发送POST请求到RoR应用程序,但找不到正确的URL

时间:2016-08-23 10:50:43

标签: android ruby-on-rails ruby ruby-on-rails-3 server

我无法使用此代码访问tests_controller.rb中的create方法。

String newUrl = "http://10.0.2.2:3000/tests";

        httpcon = (HttpURLConnection) ((new URL(newUrl).openConnection()));
        httpcon.setDoOutput(true);
        httpcon.setRequestProperty("Content-Type", "application/json");
        httpcon.setRequestProperty("Accept", "application/json");
        httpcon.setRequestMethod("POST");
        httpcon.connect();

这是我的routes.rb(我使用模型脚手架来创建RoR应用程序)。

resources :tests

我在路由或其他方面错了。当我在Android中运行此代码时,根本不运行create方法。

1 个答案:

答案 0 :(得分:0)

很难说出你失败的原因。你的路线似乎没问题。你可以检查其他事情:

  • 您的Rails服务器是否正在侦听正确的接口和端口?
  • 您的客户端正在使用的计算机与服务器之间是否存在网络问题?
  • 不要经历Android的edit-compile-deploy-run循环,只需使用curl或类似工具从控制台尝试POST请求。

当您确定问题的位置时,您可以提出另一个问题,或者更有可能已经在线找到答案。