title: Android - writing data to database using RESTful api
body: <p>I'm a beginner Android programmer and I am working on a program that reads Contacts from native device phonebook and writes them to database (  ).</p>
  
  <p>I can get and read the contacts from my mobile phone, but I'm stuck at the "writing contacts to database".
  This is the guideline I have to follow:</p>
  
  <pre><code>write function: 
  write: 
  [{'activity':'writeData', 
    'firstname':'Janis', 
    'lastname':'Berzins', 
    'telnr': '12312312'}]
  
  request should be sent as: [{'activity':'readData'}]
  
  example for read: [{"firstname":"Vards","lastname":"Uzvards","telnr":"12345678"},{"firstname":"Viesturs","lastname":"Lapsa","telnr":"11223344"}]
  </code></pre>
  
  <p>I researched countless tutorials, documentations and so on, for four days and this is what I thought should work for the sending to database part:</p>
  
  <pre><code>public static HttpResponse doPost(String url, JSONObject c) throws ClientProtocolException, IOException 
  {
      HttpParams httpParams = new BasicHttpParams();
      HttpClient httpclient = new DefaultHttpClient(httpParams);
      HttpPost request = new HttpPost(url);
      StringEntity s = new StringEntity(c.toString());
      s.setContentEncoding("UTF-8");
      s.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));
  
      request.setEntity(s);
      request.addHeader("accept", "application/json");
  
      return httpclient.execute(request);
  }
  </code></pre>
  
  <p>And to create a simple test Json I used this class:</p>
  
  <pre><code>    public class RestPost extends AsyncTask&lt;String, Void, String&gt;
  {
      @Override
      protected String doInBackground(String... params) {
          JSONObject json = new JSONObject();
  
          try
          {
              json.put("activity", "writeData");
              json.put("firstname", "Janis");
              json.put("lastname", "Berzins");
              json.put("telnr", "123123123");
  
              RestMethods.doPost(Main.server, json);
          }
  
          catch (JSONException e)
          {
              // TODO Auto-generated catch block
              e.printStackTrace();
              Log.i("Error: ", e.toString());
          }
          catch (ClientProtocolException e)
          {
              // TODO Auto-generated catch block
              e.printStackTrace();
              Log.i("Error: ", e.toString());
          }
          catch (IOException e)
          {
              // TODO Auto-generated catch block
              e.printStackTrace();
              Log.i("Error: ", e.toString());
          }
          Log.i("DONE ", " I GUES");
          return null;
      }
  }
  </code></pre>
  
  <p>But after my Android app executes this function - nothing has changed in the database ().</p>
  
  <p>So, please, can anyone help me figure out, what am I doing wrong?</p>
  
  <p>Thank you!</p>