[英]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.