Help connecting to App Engine server correctly?

61 views
Skip to first unread message

Stephen Chan

unread,
Aug 24, 2016, 9:34:32 AM8/24/16
to Google App Engine
Hi, I began using Google App Engine to create an Application Server for my mobile application. I began by hosting PHPMyAdmin onto my Application Server to manage my Google Cloud server following this link here: https://cloud.google.com/sql/docs/phpmyadmin-on-app-engine. The problem is, now I want to create a way for my Android Application to communicate with my Application Server. I simply want to be able to store data into my Application Server where I have hosted an instance of Google Cloud, and also retrieve data from my instance of Google Cloud. However, I can't seem to communicate with my application server at all. This is what I did after following the above steps:

1. I created a Request Builder where I can send a Firebase Cloud Messaging Token from my device to the Application Server like so:
public class NotificationInstanceService extends FirebaseInstanceIdService {
 
private static final String TAG = "NotificationInstance";
 
@Override
 
public void onTokenRefresh() {
 
//Getting registration token
 
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
 
//Displaying token on logcat
 
Log.d(TAG, "Refreshed token: " + refreshedToken);
 sendRegistrationToServer
(refreshedToken);
 
}

 
private void sendRegistrationToServer(String token) {
 
//You can implement this method to store the token on your server
 
//Not required for current project
 
OkHttpClient client = new OkHttpClient();
 
//Create the request body
 
RequestBody body = new FormBody.Builder().add("Token", token).build();
 
//Know where to send the request to
 
Request request = new Request.Builder().url("<Application Server URL>/register.php")
 
.post(body)
 
.build();
 
//Create
 
try {
 client
.newCall(request).execute();
 
} catch (IOException e) {
 e
.printStackTrace();
 
}

 
}

This seems to be working.
client.newCall(request.execute();
seems to go through without any errors.

1. I created a script called register.php which looks something like this:

    if (isset($_POST["Token"])) {
           $_uv_Token
=$_POST["Token"];
           $conn
= mysqli_connect("/cloudsql/<database name url>","root","","FCM") or die("Error connecting");
           $q
="INSERT INTO users (Token) VALUES ( '$_uv_Token') "
             
." ON DUPLICATE KEY UPDATE Token = '$_uv_Token';";
      var_dump
(mysqli_query($conn,$q));
      mysqli_query
($conn,$q) or die(mysqli_error($conn));
      mysqli_close
($conn);
   
} else {
        var_dump
($_REQUEST);
   
}

This would be a simple way to connect to my database and update the users database I created using the PHPMyAdmin console page. 


3. After I created the script, I simply called

appcfg.py update .
in my directory, which I would assume redeploys my instance with register.php created and I try to send my REST call via my Android Application. I notice in the Google Cloud Console, logs are posted but I don't see my data being uploaded to the users database. I am not sure where I am going wrong. Any help would be much appreciated. Thanks!

Adam (Cloud Platform Support)

unread,
Aug 26, 2016, 1:25:31 PM8/26/16
to Google App Engine
Could you please clarify what you mean by 'Google Cloud Server' and 'instance of Google Cloud'? It seems like you could be referring to either a Google Cloud SQL or a Google Compute Engine instance (which can run a SQL server) but I'm not sure.

Also, I don't understand what you mean by "store data into my Application Server where I have hosted an instance of Google Cloud", could you elaborate?
Reply all
Reply to author
Forward
0 new messages