2017-12-18 228 views
0

我跟着这个谷歌云功能教程 - https://firebase.google.com/docs/functions/get-started谷歌云计算功能+ Android应用

我能够通过我的Mac上运行的Node.js塞拉利昂部署功能。我现在可以成功地从浏览器中调用我的addMessage Http Endpoint,并成功将该消息添加到Firebase控制台中的实时数据库中。

我还实施了谷歌注册在我的应用程序按照本教程:https://developers.google.com/identity/sign-in/android/start-integrating

不过,我无法通过一个标准的HTTP请求来调用我的Android应用程序的方法addMessage()函数。这是我的Android代码:

私有类HttpSenderTask扩展的AsyncTask {

String testString; 
    String content; 

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

     testString = text.getText().toString(); 
    } 

    @Override 
    protected String doInBackground(String... strings) { 

     try{ 
      URL url = new URL("**myCloudFunctionlinkFromFirebaseConsole**/addMessage?text=" + testString); 
      HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
      connection.setRequestMethod("POST"); 
      connection.setDoOutput(true); 
      connection.setConnectTimeout(5000); 
      connection.setReadTimeout(5000); 
      connection.connect(); 

      content = "Done"; 
     } 
     catch (Exception ex){ 
      Log.d(TAG, ex.toString()); 
     } 
     return content; 
    } 

    @Override 
    protected void onPostExecute(String s) { 
     super.onPostExecute(s); 
    } 
} 

我在做什么错?

此外,是否有一些文件,可以教我们如何编写利用云功能的android代码?任何帮助在这个问题将不胜感激。

谢谢大家

回答

0

此行可能是你要去哪里错了:

URL url = new URL("myCloudFunctionlink/addMessage?text=" + testString); 

你需要建立一个URL到完整URL到HTTPS端点通过云功能曝光。您可以在部署您的功能后通过CLI获取此信息。它会将您的Firebase项目名称作为URL中主机名的一部分。

另请注意,任何查询字符串参数都应该转义,除非您确定它们对于HTTP参数是安全的。

+0

我已经使用我从Firebase控制台获得的URL添加了我的addMessage函数。它从浏览器启动时可以成功运行。但不是从android应用程序。 CLI url是其他的东西吗? – user3324792

+0

不能。请更新以显示您收到的错误。仅仅说“不起作用”是不够的。 –

+0

我没有收到任何错误。根本没有来自服务器的响应。 – user3324792