0

I'm trying to send JSON data over to a PHP service. This is the code that I have to transmit the data over:

public class TransmitRegistrationData extends AsyncTask<JSONObject, JSONObject, JSONObject> {

    String url = "http://kreationware.com/phpimp/RegistrationDataReceiver.php";

    @Override
    protected JSONObject doInBackground(JSONObject... data) {
        JSONObject json = data[0];
        try {

            DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpPost httppostreq = new HttpPost(url);
            StringEntity se = new StringEntity(json.toString());
            se.setContentType("application/json;charset=UTF-8");
            se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json;charset=UTF-8"));
            httppostreq.setEntity(se);
            HttpResponse httpresponse = httpclient.execute(httppostreq);

        } catch (Exception e) { e.printStackTrace(); }
            return json;

    }
}

And I'm populating and building said JSON data as such

public void buildRegistrationData(String data){
        try{
            jsonObject.put("registration_data", data);
            registrationJsonObject.put("registration_information", jsonObject);
        }catch (JSONException e){
            e.printStackTrace();
        }
    }

Where String data is what the user inputs. Yet I keep receiving an empty array on the PHP side. Any clue where I'm going wrong?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • http://stackoverflow.com/questions/26124087/how-to-connect-mysql-in-my-android-project/26124751#26124751 – Jamil Dec 29 '14 at 15:25

1 Answers1

0

Two thing, first you should use the magical library GSON to mapping your JSON Objects. Second, in my experience you should send your JSON strings in Base64 representation to avoid encoding issues. I preffer use Volley HTTP library when I need send data to a server.

ClarkXP
  • 1,223
  • 10
  • 23