Google Cloud Functions + Android app

Internet of Things IoT Frameworks 2 years ago

0 1 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating
_x000D_ _x000D_ 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

Posted on 16 Aug 2022, this text provides information on IoT Frameworks related to Internet of Things. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago
_x000D_ 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.

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

Important Internet of Things Links