1

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));
 }
 }
 }
asked Nov 9, 2011 at 15:41
2
  • 1
    in this code ... well ... everything is wrong ... 1. you didn't post anything 2. is.toString() will not return InputStream content but rather InputStream string representation ... 3. you have HttpClient client = new DefaultHttpClient(httpParams); first and then HttpParams httpParams = new BasicHttpParams(); in try scope ... what for ? Commented Nov 9, 2011 at 16:01
  • Yes that is indeed a major flaw in my code and I accept it. I realized it later. I have certainly removed that now. Thanks for the feedback!! Commented Nov 9, 2011 at 16:07

3 Answers 3

4

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();
...
answered Nov 9, 2011 at 15:49
4
  • 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? Commented 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 have JSONArray jsArray = new JSONArray(items2); and you have your JSON array. Commented 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? Commented 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. :) Commented Nov 9, 2011 at 16:05
1

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
answered Nov 9, 2011 at 15:52
2
  • then how do I use HttpPost ? Do I need it or not ? Commented Nov 9, 2011 at 16:00
  • You can always use toString() on the JSONObject to get its string representation Commented Nov 9, 2011 at 16:02
1

You can create a JSONArray from your ArrayList

JSONArray foo = new JSONArray(yourArrayList);
answered Nov 9, 2011 at 15:53
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.