【问题标题】:Connection to mysql database failed while trying to run ionic project on android尝试在 android 上运行 ionic 项目时连接到 mysql 数据库失败
【发布时间】:2020-06-20 22:48:13
【问题描述】:

我正在尝试使用 nodejs 作为服务器来运行 ionic 应用程序以连接到 android 上的 mysql 数据库,该应用程序在实验室中运行完美,但是当涉及到 android 时,它无法连接到 localhost,显然模拟器无法识别正在使用的地址。这是我在离子服务中使用的代码:


export class CandidatService {
  base_path = 'http://10.0.2.2:1617';

  constructor(private http: HttpClient) { }

  // Http Options
  httpOptions = {
    headers: new HttpHeaders({
      'Content-Type': 'application/json'
    })
  }

  getAllCandidats(): Observable<Candidat[]>
  {
    return this.http
      .get<Candidat[]>(this.base_path+"/selectAllCandidats", this.httpOptions)
      .pipe()
  }
}```

【问题讨论】:

  • 你能分享错误日志吗?
  • 您的移动应用程序和您的 nodejs 服务器应该在同一个 wifi 中运行,您可以连接到这个 IP 地址,或者正如答案中提到的那样,您可以使用 ngrok 服务获取 https URL,或者您需要将 nodejs 服务器部署在 AWS、heroku 等云提供商中的第三个选项

标签: android mysql node.js cordova ionic-framework


【解决方案1】:

这个 ip 和端口是有效的 nodejs 服务器应用吗?

http://10.0.2.2:1617

你有关于 nodejs 上发生了什么的 console.log 吗? 你的nodejs服务器和你的android在same wifi上运行吗?

如果不是,那么您可能需要使用公共 ip 或者简单的是使用ngrok

参考:https://ngrok.com/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-13
    • 2011-12-05
    • 1970-01-01
    相关资源
    最近更新 更多