2011-11-09 205 views
1

我有一個包含某些值的「String」類型的ArrayList。我想使用Web服務將此ArrayList發送到遠程數據庫。我打算使用JSON來插入相同的內容,但我面臨一些困難並想知道如何去做。通過web服務從Android應用程序發送ArrayList到遠程數據庫

這裏是我目前的代碼:

我有我的ArrayList定義像這樣。它的目的是存儲複選框的值

 ArrayList<String> items2 = new ArrayList<String>(); 

      for (int i = 0; i < arr2.length; i++) 
      { 
       if (checkedabsent.get(i)) 
       { 
        items2.add(arr2[i]); //arr2 is a String array containing all values 
        System.out.println(items2); 
       } 
      } 

      Log.d("", "items:"); 
      for (String string : items2) 
      { 
       Log.d("", string); 
       System.out.println(items2); 
      } 

這是我發現很難!該JSON代碼

HttpPost httppost = new HttpPost("http://10.0.2.2/enterdata/Service1.asmx"); 
    HttpClient client = new DefaultHttpClient(httpParams); 

    try 
      { 
      String result; 
      JSONObject obj = new JSONObject(); 
      // how do I put the array list over here ??   

      int TIMEOUT_MILLISEC = 10000; // = 10 seconds 
      HttpParams httpParams = new BasicHttpParams(); 
      HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC); 
      HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC); 

      httppost.setHeader("Content-Type", "application/json"); 
      httppost.setHeader("Accept","application/json"); 

      // Execute HTTP Post Request 
      HttpResponse httpResponse = client.execute(httppost); 
      HttpEntity httpEntity = httpResponse.getEntity(); 


      if (httpEntity != null) 
      { 
       InputStream is = httpEntity.getContent(); 
       result = is.toString(); 
       Log.i("Result is ", "Result: " + result); 
       if(result.equalsIgnoreCase("success")) 
       { 
        startActivity(new Intent(Mark.this,nextscreen.class)); 
       } 
      } 

      } 
+1

一個JSONArray ......嗯......一切都是錯的... 1.你沒有張貼任何2. is.toString ()不會返回InputStream內容,而是InputStream字符串表示... 3.你有'HttpClient客戶端=新的DefaultHttpClient(httpParams);'第一個然後'HttpParams httpParams = new BasicHttpParams();'在嘗試範圍內......爲? – Selvin

+0

是的,這確實是我的代碼中的一個主要缺陷,我接受它。我後來意識到它。我現在肯定已經刪除了。感謝您的反饋!! –

回答

4

要轉換成JSON數組,你會使用以下

ArrayList<String> list = new ArrayList<String>(); 
list.add("Item 1"); 
list.add("Item 2"); 
JSONArray jsArray = new JSONArray(list); 

然後通過jsArraydescribed here

對不起,如果我誤解了你的問題。

更新,所以:

ArrayList<String> items2 = new ArrayList<String>(); 

     for (int i = 0; i < arr2.length; i++) 
     { 
      if (checkedabsent.get(i)) 
      { 
       items2.add(arr2[i]); //arr2 is a String array containing all values 
       System.out.println(items2); 
      } 
     } 

     Log.d("", "items:"); 
     for (String string : items2) 
     { 
      Log.d("", string); 
      System.out.println(items2); 
     } 

     HttpPost httppost = new HttpPost("http://10.0.2.2/enterdata/Service1.asmx"); 
     HttpClient client = new DefaultHttpClient(httpParams); 

     try 
     { 
      String result; 
      //JSONObject obj = new JSONObject(); 
      JSONArray jsArray = new JSONArray(items2); 

      int TIMEOUT_MILLISEC = 10000; // = 10 seconds 
      HttpParams httpParams = new BasicHttpParams(); 

... 
+0

你不需要對不起老兄!所以這意味着我必須爲每個選中的項目編寫list.add()語句,不是嗎? 。我可以放一個循環來添加列表中的所有項目嗎?如果是這樣,怎麼樣?就我的代碼而言,我還需要做些什麼改變? –

+0

您是否已經從您的代碼中填充了ArrayList:'ArrayList items2 = new ArrayList ();',所以你只需要'JSONArray jsArray = new JSONArray(items2);'並且你有你的JSON數組。 – Ricky

+0

我已更新我的答案,希望它有幫助。 – Ricky

1

可你只是做到這一點?

ArrayList<String> items2 = new ArrayList<String>(); 
... 

JSONObject obj = new JSONObject(); 

for(int i=0; i<items2.size(); i++) 
{ 
    obj.put("" + i, items.get(i)); 
} 
//then use obj.write(Writer x) to send it 
+0

那我該如何使用HttpPost?我需要與否? –

+0

您可以隨時在JSONObject上使用'toString()'來獲取其字符串表示形式 –

+0

非常感謝您的幫助! :-) –

1

您可以從您的ArrayList

JSONArray foo = new JSONArray(yourArrayList); 
在此代碼
+0

thnx的幫助! :-) –

相關問題