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));
}
}
}
3 Answers 3
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();
...
-
You need not be sorry dude !! So that means I have to write list.add() Statements for every checked item isn't it? . Can I put a loop that will add all items in the list? If so, how ? and also what changes do I need to do as far as my code is concerned?Parth Doshi– Parth Doshi2011年11月09日 15:55:53 +00:00Commented Nov 9, 2011 at 15:55
-
Haven't you already filled the ArrayList, from your code:
ArrayList<String> items2 = new ArrayList<String>();
, so you'd just haveJSONArray jsArray = new JSONArray(items2);
and you have your JSON array.Ricky– Ricky2011年11月09日 15:57:32 +00:00Commented Nov 9, 2011 at 15:57 -
ya it's populated fine I get it !! :-) One more thing I want to ask is that once the array goes as a parameter to my web service , I can always write an insert into query in my WebService code to put individual values of array "items2" in my database right?Parth Doshi– Parth Doshi2011年11月09日 16:04:09 +00:00Commented Nov 9, 2011 at 16:04
-
Of course you can. :) You'll treat it as an array in your web service and do what you jolly well like with it. :)Ricky– Ricky2011年11月09日 16:05:35 +00:00Commented Nov 9, 2011 at 16:05
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
-
then how do I use HttpPost ? Do I need it or not ?Parth Doshi– Parth Doshi2011年11月09日 16:00:36 +00:00Commented Nov 9, 2011 at 16:00
-
You can always use
toString()
on the JSONObject to get its string representationAleks G– Aleks G2011年11月09日 16:02:08 +00:00Commented Nov 9, 2011 at 16:02
You can create a JSONArray
from your ArrayList
JSONArray foo = new JSONArray(yourArrayList);
Explore related questions
See similar questions with these tags.
HttpClient client = new DefaultHttpClient(httpParams);
first and thenHttpParams httpParams = new BasicHttpParams();
in try scope ... what for ?