我有一個包含某些值的「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));
}
}
}
一個
JSONArray
......嗯......一切都是錯的... 1.你沒有張貼任何2. is.toString ()不會返回InputStream內容,而是InputStream字符串表示... 3.你有'HttpClient客戶端=新的DefaultHttpClient(httpParams);'第一個然後'HttpParams httpParams = new BasicHttpParams();'在嘗試範圍內......爲? – Selvin是的,這確實是我的代碼中的一個主要缺陷,我接受它。我後來意識到它。我現在肯定已經刪除了。感謝您的反饋!! –