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));
                }
            }

            }
Parth Doshi
  • 4,200
  • 15
  • 79
  • 129
  • 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 ? – Selvin Nov 09 '11 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!! – Parth Doshi Nov 09 '11 at 16:07

3 Answers3

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();

...
Community
  • 1
  • 1
Ricky
  • 7,785
  • 2
  • 34
  • 46
  • 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 Nov 09 '11 at 15:55
  • Haven't you already filled the ArrayList, from your code: `ArrayList items2 = new ArrayList();`, so you'd just have `JSONArray jsArray = new JSONArray(items2);` and you have your JSON array. – Ricky Nov 09 '11 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 Nov 09 '11 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 Nov 09 '11 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
Aleks G
  • 56,435
  • 29
  • 168
  • 265
1

You can create a JSONArray from your ArrayList

JSONArray foo = new JSONArray(yourArrayList);
Ben Weiss
  • 17,182
  • 6
  • 67
  • 87