【问题标题】:Lack of access to localhost laravel project API route in android studio在 android studio 中无法访问 localhost laravel 项目 API 路由
【发布时间】:2019-03-31 21:00:20
【问题描述】:

我有一个 laravel 项目包含一个 RestFull API。我想从 android studio 访问这个 API。我使用retrofit 库。当我通过artisan 运行服务器时,无法访问localhost laravel 应用程序路由到android studio。根据这个问题accessing-localhost-laravel-app-routes-to-android-studio-failed,我将基本url 更改为http://192.168.1.103:8000/halamooz/public/api/ 并运行带有apache 服务器的laravel 应用程序。对于这项工作,我遵循此指南Laravel practical guide for using XAMPP。当然我不使用XAMPP 并单独安装apache 服务器。我从浏览器访问网络路由,但是当我尝试从postman 或android studio 访问API 路由时,我收到404 page not founded 错误。我的错误是什么? 我把这行放在httpd-vhosts 文件中:

 <VirtualHost *:80>
DocumentRoot "C:/apache/htdocs/halamooz/public"
ServerName exampledomain.com
ServerAlias exampledomain.com
<Directory "C:/apache/htdocs/halamooz/public">
AllowOverride All
Require all Granted
</Directory>
</VirtualHost>

【问题讨论】:

    标签: android laravel apache localhost


    【解决方案1】:

    在httpd.conf文件中找到

    Listen 80 
    

    下面写着

    Listen 8060
    

    然后转到 httpd-vhosts.conf

    <VirtualHost *:8060>
        ServerAdmin webmaster@dummy-host2.example.com
        DocumentRoot "C:/apache/htdocs/halamooz/public"
        ServerName dummy-host2.example.com
        ErrorLog "logs/dummy-host2.example.com-error.log"
       CustomLog "logs/dummy-host2.example.com-access.log" common
    </VirtualHost>
    

    将 DocumentRoot 替换为您的项目路径。希望对你有帮助

    【讨论】:

    • 点击 localhost:8060 时出现什么错误?首先检查你的 apache 和 localhost 服务器是否正常工作
    • 我把你的代码放在 httpd-vhosts.conf 文件中,当我在浏览器中测试 localhost:8060 时,它显示了一些文件夹的列表,但是当我在邮递员中尝试时收到 404 错误跨度>
    • 在邮递员中传递以下标题并重试 Content-type: application/json;
    • 我做到了,但甚至没有工作。我在邮递员中给出了这个网址:http://localhost:8060/halamooz/public/api/login
    • 给它:The requested URL /api/login was not found on this server.
    【解决方案2】:

    我终于完成了这些步骤,我的问题解决了。

    • 第 1 步: 将您的 laravel 项目放入 C:\apache\htdocs
    • 第 2 步:以管理员身份运行 notepad 应用程序并从 C:\Windows\System32\drivers\etc 路径打开 hosts 文件并将此行添加到文件末尾。

    127.0.0.1 exampledomain.com www.exampledomain.com

    • 第 3 步:从 C:\apache\conf\extra 路径打开 httpd-vhosts 文件并将这些行添加到文件末尾:

      <VirtualHost *:80>
      DocumentRoot "C:/apache/htdocs/NameOfProject/public"
      ServerName exampledomain.com 
      ServerAlias exampledomain.com 
      <Directory "C:/apache/htdocs/NameOfProject/public">
      AllowOverride All
      Require all Granted
      

    • 第 4 步:从 C:\apache\conf\httpd.conf 文件中取消注释此行。

    包括 conf/extra/httpd-vhosts.conf

    • 第 5 步:使用此 Url 访问postman 或 android studio 项目中的 API。

    http://{您计算机的 IPv4}:8000/{ProjectName}/public/api/{desired method} 例如: http://192.168.1.3:8000/halamooz/public/api/login

    • 注意:您的 Android 设备和计算机必须在同一网络 Internet 上。

    【讨论】:

      猜你喜欢
      • 2018-09-25
      • 2018-10-22
      • 1970-01-01
      • 2011-08-19
      • 2014-06-25
      • 1970-01-01
      • 2014-11-25
      • 2014-07-18
      • 2018-06-27
      相关资源
      最近更新 更多