0

I am trying to send a JSON data from an android app to a php server. I simply asked the server to print the raw data but it is not shown on the web. I cannot figure out what might be the cause. DisplayMessageActivity.java:

public class DisplayMessageActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_message);
    new SaveTheFeed().execute();
}

class SaveTheFeed extends AsyncTask<Void, Void, Void> {

    @Override
    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);
        // Create the JSONObject
        JSONObject request = CreateRequest(message, longitude, latitude);
        //JSONObject response = null;
        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.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
            client.setRequestMethod("POST");
            client.connect();
            Log.d("doInBackground(Request)", request.toString());
            // Send the JSON object to the server
            OutputStreamWriter writer = new OutputStreamWriter(client.getOutputStream());
            String output = request.toString();
            writer.write(output);
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            client.disconnect();
        }
        return null;
    }

    private JSONObject CreateRequest(String message, double longitude, double latitude) {
        JSONObject request = new JSONObject();
        try {
            request.put("message", message);
            request.put("longitude", longitude);
            request.put("latitude", latitude);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return request;
    }
}
}

The php code:

<!DOCTYPE html>
<html>
<body>
<h2>This is your current location :)
<?php
      $json = file_get_contents('php://input');
      $request = json_decode($json, true);
      echo ($json);
?>
</h2>
<div id="map"></div>
</body>
</html>
ZigZagZebra
  • 1,349
  • 3
  • 14
  • 25
  • Or how do I even know whether the emulator actually sends the data to the url? Now I am not sure which side is having the problem. – ZigZagZebra Nov 16 '15 at 05:03
  • check the respose code from client side by client.getResponseCode() and you can trace the issue – Santhosh Nov 16 '15 at 05:11
  • Why are you sending a String of the json object to the server? Refer this http://androidexample.com/How_To_Make_HTTP_POST_Request_To_Server_-_Android_Example/index.php?view=article_discription&aid=64&aaid=89 – Abx Nov 16 '15 at 05:24
  • @San I checked the response code and it is 200. I think this means the connection succeed? But it still doesn't post message to the server. – ZigZagZebra Nov 17 '15 at 02:22

1 Answers1

0

I think what you missed in your code is to encode your string using URLEncoder. Instead of just writing the raw string encode the string before writing it. Try replacing this line:

writer.write(output)

with this line

writer.write(URLEncoder.encode(output,"UTF-8");

and let me know the output. You can also refer to this link in stackoverflow

Update:

Also don't forget to log the server response in your logcat window. To do that simply add the following lines of code:

        String line="";
        BufferedReader reader=new BufferedReader(new InputStreamReader(conn.getInputStream()));
        while ((line=reader.readLine())!=null){
            response+=line;
        }
        Log.d("response from server",response);
        reader.close();
Community
  • 1
  • 1
Abhisek Lamsal
  • 473
  • 1
  • 5
  • 16