How to send contact data to server database through REST API in Android

I’m new to Android development and I’m building an app that gets contacts from the phone’s address book and saves them to a remote database using REST calls.

I can successfully read contacts from the device but I’m having trouble with the database insertion part. Here’s what the API expects:

Data format for saving:

[{'operation':'saveContact', 
  'name':'John', 
  'surname':'Smith', 
  'phone': '98765432'}]

Request format for reading:

[{'operation':'getContacts'}]

I’ve been trying different approaches but my POST request doesn’t seem to work. Here’s my current implementation:

public static HttpResponse sendPostRequest(String endpoint, JSONObject data) throws ClientProtocolException, IOException 
{
    HttpParams params = new BasicHttpParams();
    HttpClient client = new DefaultHttpClient(params);
    HttpPost postRequest = new HttpPost(endpoint);
    StringEntity entity = new StringEntity(data.toString());
    entity.setContentEncoding("UTF-8");
    entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));

    postRequest.setEntity(entity);
    postRequest.addHeader("accept", "application/json");

    return client.execute(postRequest);
}

And here’s my async task:

public class ContactUploader extends AsyncTask<String, Void, String>
{
    @Override
    protected String doInBackground(String... args) {
        JSONObject contactData = new JSONObject();

        try
        {
            contactData.put("operation", "saveContact");
            contactData.put("name", "John");
            contactData.put("surname", "Smith");
            contactData.put("phone", "98765432");

            NetworkHelper.sendPostRequest(Config.apiUrl, contactData);
        }
        catch (JSONException ex)
        {
            ex.printStackTrace();
            Log.e("JSON Error: ", ex.toString());
        }
        catch (ClientProtocolException ex)
        {
            ex.printStackTrace();
            Log.e("Protocol Error: ", ex.toString());
        }
        catch (IOException ex)
        {
            ex.printStackTrace();
            Log.e("IO Error: ", ex.toString());
        }
        Log.d("COMPLETE ", "Request finished");
        return null;
    }
}

The code runs without errors but no data appears in the database. What could be wrong with my approach?

Your code runs fine, but you’re probably not checking what the server’s actually sending back. Check the HTTP status code and response body - the server might be throwing errors you’re not catching. Also double-check that your Config.apiUrl is right and actually reachable. I had the same thing happen where everything looked good locally, but the server was spitting out 500 errors because the database connection was busted. Add some logging to see the response status and body - that’ll show you exactly what’s going wrong server-side.

your API expects an array but u’re sending a single JSON object. Wrap your contactData in a JSONArray before sending - that’s probably why the server’s rejecting it. also check ur server logs to see if the request is even getting through.

Your code has a mismatch - the server expects an array but you’re sending a single JSONObject. Create a JSONArray and add your contact object to it. Also, you’re using the deprecated Apache HTTP client. Android dropped support in API 23, so switch to OkHttp or HttpURLConnection. Make sure you’re verifying the HTTP status code in the response to confirm the server processed your request successfully.