我是一名初学者 Android 程序员,我正在开发一个程序,该程序从本机设备电话簿中读取联系人并将其写入数据库 ( )。
我可以从我的手机获取和读取联系人,但我被困在“将联系人写入数据库”。这是我必须遵循的准则:
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"}]
我研究了无数教程、文档等,花了四天时间,这就是我认为应该用于发送到数据库部分的内容:
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);
}
为了创建一个简单的测试 Json,我使用了这个类:
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;
}
}
但是在我的 Android 应用程序执行此功能后 - 数据库 () 中没有任何变化。
所以,拜托,谁能帮我弄清楚,我做错了什么?
谢谢!