I have an ArrayList of type "String" that contain certain values. I want to send this ArrayList to a remote database using a web service. I plan to use JSON to insert the same but I am facing certain difficulties and want to know to go about it.
Here is my code at present:
I have my ArrayList defined like this. It's purpose is to store checkbox values
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);
}
Here is what I am finding difficult !! The JSON code
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));
}
}
}
To convert to a JSON array, you'd use the following
ArrayList<String> list = new ArrayList<String>();
list.add("Item 1");
list.add("Item 2");
JSONArray jsArray = new JSONArray(list);
Then pass jsArray
like described here .
Sorry if I've misunderstood your question.
Update, so:
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();
...
Can you just do this?
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
您可以从ArrayList
创建一个JSONArray
JSONArray foo = new JSONArray(yourArrayList);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.