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

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

...
Sign up to request clarification or add additional context in comments.

4 Comments

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?
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.
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?
Of course you can. :) You'll treat it as an array in your web service and do what you jolly well like with it. :)
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

2 Comments

then how do I use HttpPost ? Do I need it or not ?
You can always use toString() on the JSONObject to get its string representation
1

You can create a JSONArray from your ArrayList

JSONArray foo = new JSONArray(yourArrayList);

Comments

Your Answer

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.