I followed this Google Cloud Function tutorial - https://firebase.google.com/docs/functions/get-started
And I was able to deploy the function through node.js on my Mac running Sierra. I am now successfully able to call my addMessage Http Endpoint from a browser, and it successfully adds the message onto my real-time database in the Firebase console.
I have also implemented Google Sign In in my app as per this tutorial: https://developers.google.com/identity/sign-in/android/start-integrating
However, I am unable to call the addMessage() function from my android app through a standard Http Request. This is my android code:
private class HttpSenderTask extends 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);
}
}
What am I doing wrong?
Also, is there some documentation that can teach us how to write android code for utilizing Cloud Functions? Any help in this matter would be greatly appreciated.
Thank you all
答案 0 :(得分:0)
This line is probably where you're going wrong:
URL url = new URL("myCloudFunctionlink/addMessage?text=" + testString);
You need to build a URL to the full URL to the HTTPS endpoint exposed by Cloud Functions. You can get this from the CLI after it deploys your function. It will involve your Firebase project name as part of the host name in the URL.
Also note that any query string parameters should be escaped, unless you're sure they're safe for HTTP parameters.