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 ( ).
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:
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"}]
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:
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);
}
And to create a simple test Json I used this class:
public class RestPost extends AsyncTask<String, Void, String>
{
@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;
}
}
But after my Android app executes this function - nothing has changed in the database ().
So, please, can anyone help me figure out, what am I doing wrong?
Thank you!