2

Let's say I store a list of names , for eg: "abc","bcd","gdf"... in an array of Strings. I have an Android app that displays each of those values along with a checkbox. I need to convert my String array into a JSON String so that I can store it in a remote database. Right now I am working on localhost with a database created using SQL Server. I need to insert the JSON string values in the database using a web service , preferably SOAP

How should I do this ? Is there any other better way to do so ?

Here is my Android code.

Thanks

Parth Doshi
  • 4,200
  • 15
  • 79
  • 129

3 Answers3

2

In my case this works fine,

          JSONObject jsonObject = new JSONObject();

            jsonObject.put("key1", value1);
            jsonObject.put("key2", value2);

            JSONArray jArrayParam = new JSONArray();
            jArrayParam.put(jsonObject);

            List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
            nameValuePair.add(new BasicNameValuePair("bulkdata",
                    jArrayParam.toString()));

            Log.e("bulkdata", jArrayParam.toString());

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("yor remote database url");

        httppost.addHeader("Content-Type", "application/x-www-form-urlencoded");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);
        // get response entity
        HttpEntity entity = response.getEntity();

Try it. Thnx.

user370305
  • 108,599
  • 23
  • 164
  • 151
  • Hey thanks for posting !! I just want to know something regarding ur code..What is "key1" and "key2" . Is the remote database url , the web service url that I have used in my code ? – Parth Doshi Aug 29 '11 at 13:47
  • key1 and key2 are keys by which your data inserted into remote database. at the server side you get the data by these keys. and your database server url is pass here -> HttpPost httppost = new HttpPost("yor remote database url"); – user370305 Aug 29 '11 at 13:53
  • Actually Right now i m working on localhost ..I have my database created in SQL Server 2008. So in that case what should be my url ? Also is your code converting a String array to JSON array ? Sorry I am very new to Android and especially JSON. Please can u help me ? – Parth Doshi Aug 29 '11 at 13:56
  • here in jsonObject.put("key1", value1); jsonObject.put("key2", value2); the value1 and value2 are the elements of your string[] like string[0] and string[1] values, so it is in string format so no need to convert just pass as a string. and for the url of your local host machine, I think it is 10.0.2.2 or may be 10.0.2.1 loop back address. – user370305 Aug 29 '11 at 14:00
  • and say if my String array has upto 70 values..den I should put it in a for loop right ? – Parth Doshi Aug 29 '11 at 14:05
  • Also where should I write ur code in my app ? should I create a new java class for it ? – Parth Doshi Aug 29 '11 at 14:08
  • @user370305 let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/2957/discussion-between-parth-90-and-user370305) – Parth Doshi Aug 29 '11 at 14:10
  • its depends on your class and coding style, I think just put the code in the method from where you call your remote database insertion. – user370305 Aug 29 '11 at 14:11
1

Well, I just tried to show you how to write the String array to JSONObject and JSONArray.

String arr[]  = {"1","parth","present","30-82011","Mumbai"};

try {
                JSONObject obj=new JSONObject();
                obj.put("rollno",new Integer(arr[0]));
                obj.put("status",arr[1]);
                obj.put("date",arr[2]);
                obj.put("place",arr[3]);
                System.out.print(obj.toString(1));

                JSONArray list = new JSONArray();
                list.put(arr[0]);
                list.put(arr[1]);             
                list.put(arr[2]);               
                list.put(arr[3]);              
                System.out.print(list.toString(1));
                System.out.println("");
        } catch (Exception e) {
            e.printStackTrace();
        }
halfer
  • 19,824
  • 17
  • 99
  • 186
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
0
    var arr:String = com.adobe.serialization.json.JSON.encode(Obj);
var data_projects:Array = stmt.getResult().data;
var b_data:String = com.adobe.serialization.json.JSON.encode(data_projects);
var arr:String = com.adobe.serialization.json.JSON.encode(data_projects);
var arr1:Object = com.adobe.serialization.json.JSON.decode(b_data) as Array;

             for(var d:int=0;d<=data_projects.length-1;d++)
                 
                {
//Mapping properties of Proxy classes with actual fields
                var bbb:Object = new Object;
                data.MdId = arr1[d].MD_ID;
                data.MdDevId=arr1[d].MD_DEVICE_ID;  
                data.MdRecId=arr1[d].MD_REC_ID;
                data.MdPrjId=   arr1[d].MD_PRJ_ID   ;
                data.MdMbcId    =   arr1[d].MD_MBC_ID;
                data.MdMbcValue= arr1[d].MD_MBC_VALUE;
                data.MdParentRecId= arr1[d].MD_MBC_ID;
//below is the create method on the WSDL
                ws.Create(data);

                }
johnny
  • 2,032
  • 1
  • 25
  • 45
  • it is in action script, in flash builder, I insert data to sql server from sqlite using (.Net) Web service. – johnny Aug 20 '13 at 15:51