2011-08-29 114 views
2

假設我在字符串數組中存儲了一個名稱列表,例如:「abc」,「bcd」,「gdf」...。我有一個Android應用程序,顯示每個值以及一個複選框。我需要將我的字符串數組轉換爲JSON字符串,以便我可以將其存儲在遠程數據庫中。現在我正在使用使用SQL Server創建的數據庫來處理本地主機。我需要使用Web服務在數據庫中插入JSON字符串值,最好是SOAP如何使用JSON將記錄插入到SQL數據庫中

我該怎麼做?還有其他更好的方法嗎?

這是我的Android code

感謝

回答

2

在我的情況下能正常工作,

  JSONObject jsonObject = new JSONObject(); 

      jsonObject.put("key1", value1); 
      jsonObject.put("key2", value2); 

      JSONArray jArrayParam = new JSONArray(); 
      jArrayParam.put(jsonObject); 

      List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(); 
      nameValuePair.add(new BasicNameValuePair("bulkdata", 
        jArrayParam.toString())); 

      Log.e("bulkdata", jArrayParam.toString()); 

     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost("yor remote database url"); 

     httppost.addHeader("Content-Type", "application/x-www-form-urlencoded"); 
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8)); 
     // Execute HTTP Post Request 
     HttpResponse response = httpclient.execute(httppost); 
     // get response entity 
     HttpEntity entity = response.getEntity(); 

試試吧。日Thnx。

+0

嘿謝謝張貼!我只想知道一些關於你的代碼的東西。什麼是「key1」和「key2」。遠程數據庫網址是我在代碼中使用的網絡服務網址嗎? –

+0

key1和key2是通過其將數據插入遠程數據庫的密鑰。在服務器端,您可以通過這些鍵獲取數據。你的數據庫服務器的URL是通過這裏 - > HttpPost httppost =新的HttpPost(「yor遠程數據庫url」); – user370305

+0

其實現在我在本地主機工作..我有我的數據庫在SQL Server 2008中創建。所以在這種情況下應該是我的網址?你的代碼是否將字符串數組轉換爲JSON數組?對不起,我對Android很陌生,特別是JSON。請你能幫助我嗎? –

1

嗯,我只是試圖告訴你如何將字符串數組寫入JSONObject和JSONArray。希望這也能幫助你。

String arr[] = {"1","parth","present","30-82011","Mumbai"}; 

try { 
       JSONObject obj=new JSONObject(); 
       obj.put("rollno",new Integer(arr[0])); 
       obj.put("status",arr[1]); 
       obj.put("date",arr[2]); 
       obj.put("place",arr[3]); 
       System.out.print(obj.toString(1)); 

       JSONArray list = new JSONArray(); 
       list.put(arr[0]); 
       list.put(arr[1]);    
       list.put(arr[2]);    
       list.put(arr[3]);    
       System.out.print(list.toString(1)); 
       System.out.println(""); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
0
var arr:String = com.adobe.serialization.json.JSON.encode(Obj); 
var data_projects:Array = stmt.getResult().data; 
var b_data:String = com.adobe.serialization.json.JSON.encode(data_projects); 
var arr:String = com.adobe.serialization.json.JSON.encode(data_projects); 
var arr1:Object = com.adobe.serialization.json.JSON.decode(b_data) as Array; 

       for(var d:int=0;d<=data_projects.length-1;d++) 
         
       { 
//Mapping properties of Proxy classes with actual fields 
       var bbb:Object = new Object; 
       data.MdId = arr1[d].MD_ID; 
       data.MdDevId=arr1[d].MD_DEVICE_ID; 
       data.MdRecId=arr1[d].MD_REC_ID; 
       data.MdPrjId= arr1[d].MD_PRJ_ID ; 
       data.MdMbcId = arr1[d].MD_MBC_ID; 
       data.MdMbcValue= arr1[d].MD_MBC_VALUE; 
       data.MdParentRecId= arr1[d].MD_MBC_ID; 
//below is the create method on the WSDL 
       ws.Create(data); 

       } 
+0

它是在動作腳本中,在Flash Builder中,我使用(.Net)Web服務從sqlite將數據插入到sql server。 – johnny

相關問題