2013-04-13 31 views
0

我是一名初學Android程序員,我正在從本地設備電話簿中讀取聯繫人並將其寫入數據庫()的程序。Android - 使用RESTful api將數據寫入數據庫

我可以從手機上獲取和閱讀聯繫人,但我被困在「寫數據庫聯繫人」。 這是指導我必須遵循:

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應用程序後,執行此功能 - 沒有在數據庫中已改變()。

所以,請誰能幫我弄清楚,我做錯了什麼?

謝謝!

回答

0

你需要一個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); 

你也許可以一次發送多個對象到服務器 - 即使「規範「沒有提及它,使用數組是一個暗示,你可以。

+0

我改變了方法 - 發送JASONArray,但仍沒有接縫被髮送到數據庫:( – EdvardsMC

+0

那麼究竟什麼是到達服務器你有沒有嘗試獲得通過Wireshark的或至少是數據。 ?在服務器端的一些記錄 –

+0

我不是Wireshark的那麼好,但我可以看到有我的aplication發出此: 7.674770000 \t 10.2.5.231 \t 194.213.101.181 \t HTTP POST/API。 PHP的HTTP/1.1(應用程序/ JSON) – EdvardsMC

0

好吧,我得到它與發送到數據庫的工作!:) 現在我正在使用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(); 
}