0

I have an Android app that sends a http post to a remote server:

 protected Void doInBackground(Void... params) {
        // Get the message from the intent
        Intent intent = getIntent();
        String message = intent.getStringExtra(MapsActivity.EXTRA_MESSAGE);
        double longitude = intent.getDoubleExtra(MapsActivity.EXTRA_LONGITUDE, 0.0);
        double latitude = intent.getDoubleExtra(MapsActivity.EXTRA_LATITUDE, 0.0);
        Log.d("doInBackground", message);
        Log.d("doInBackground", String.valueOf(longitude));
        Log.d("doInBackground", String.valueOf(latitude));
        URL url = null;
        HttpURLConnection client = null;
        try {
            // Establish http connection
            url = new URL("http://******.com/");
            client = (HttpURLConnection) url.openConnection();
            client.setDoOutput(true);
            client.setDoInput(true);
            client.setRequestMethod("POST");
            client.connect();
            OutputStreamWriter writer = new OutputStreamWriter(client.getOutputStream());
            String output;
            output = URLEncoder.encode("message", "UTF-8")
                    + "=" + URLEncoder.encode(message, "UTF-8");

            output += "&" + URLEncoder.encode("longitude", "UTF-8") + "="
                    + URLEncoder.encode(String.valueOf(longitude), "UTF-8");

            output += "&" + URLEncoder.encode("latitude", "UTF-8") + "="
                    + URLEncoder.encode(String.valueOf(latitude), "UTF-8");
            Log.d("doInBackground(output)", output);
            Log.d("doInBackground(code)", String.valueOf(client.getResponseCode()));  // Return 200
            writer.write(output);
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            client.disconnect();
        }
        return null;
    }

In the server, I have:

    <?php
      $m = urldecode($_POST['message']);
      $long = urldecode($_POST['longitude']);
      $lat = urldecode($_POST['latitude']);
      print " ==== POST DATA =====
      Message  : $m
      Longitude : $long
      Latitude  : $lat"; 
?>

client.getResponseCode() returns 200, I think that means my connection was successful? But the website still shows nothing. What might cause the problem? I got

E/GMPM: getGoogleAppId failed with status: 10
E/GMPM: Uploading is not possible. App measurement disabled

might this be the problem?

ZigZagZebra
  • 1,349
  • 3
  • 14
  • 25
  • Do you have any specific reason why not to use some library like Retrofit? – Eduardo Bonet Nov 17 '15 at 02:53
  • No. I don't know this library. :( I thought what I was doing was one of simplest ways. I am just testing whether I could send some data from an Android app to a remote server. I used JSON to pass the data originally but it didn't work out. So I am trying to find another way to walk around. (This is my original post: http://stackoverflow.com/questions/33728721/json-data-sent-from-android-does-not-show-on-the-server) Is there any reason why you recommend this library? – ZigZagZebra Nov 17 '15 at 02:58
  • This is the most used library for Network calls. It is opensourced by Square, and has active development and a lot of documentation on the internet. It automagically marshals JSON, deals with headers, body and async. If there's one library every app should have, it's this one. – Eduardo Bonet Nov 17 '15 at 19:16

1 Answers1

0

What do you mean by the website doesn't show anything? You cannot see the print when you reload the web site because you are not saving it anywhere, you are simply printing out the values on that one single request. To debug you can write the post params to a file instead to see if they are coming through or better yet log the returned object on the android side.

Zach
  • 1,964
  • 2
  • 17
  • 28