[英]Android - writing data to database using RESTful api
我是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应用执行完此功能后-数据库()中没有任何更改。
所以,请,有人可以帮我弄清楚我在做什么错吗?
谢谢!
您需要一个json数组(请参阅规范中json周围的“ [“和”]“字符。
JSONArray jsonArray = new JSONArray();
// your code to create the JSONObject
// ...
jsonArray.put(json);
// use this within your RestMethods call
RestMethods.doPost(Main.server, jsonArray);
您可能可以一次将多个对象发送到服务器-即使“规范”没有提及,使用数组也可以提示您。
好的,我可以将其发送到数据库了! :)现在,我正在使用Get方法..这就是我得到的-现在看来似乎无法正常工作,可能是由于数据库中的数据作为JSONArray发送的事实?
public static JSONObject doGet(String url)
{
JSONObject json = null;
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
httpget.addHeader("accept", "application/json");
HttpResponse response;
try {
response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null)
{
InputStream instream = entity.getContent();
String result= convertStreamToString(instream);
json=new JSONObject(result);
instream.close();
}
}
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());
}
catch (JSONException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
Log.i("Error: ", e.toString());
}
return json;
}
public static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try
{
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
}
catch (IOException e)
{
e.printStackTrace();
Log.i("Error: ", e.toString());
}
finally
{
try
{
is.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
return sb.toString();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.