Is it possible to send Arraylist as value to a single key in BasicValuePair in android

4.2k Views Asked by At

Hi i have multi dimensional array format data.I need to pass these datas with basicnamevaluepair to POST method.Is there any possible to pass the entire arraylist as value to single key in android.

ex:
 the arrayList is 

 ArrayList<String> data=new ArrayList<String>();
 data.add("Datas");
 data.add("Datas2");
 data.add("Datas3");

Is it possible to pass arraylist like this.Passing values in ArrayList using BasicNameValuePair.

List<BasicNameValuePair> pairedData=new ArrayList<BasicNameValuepair();
     pairedDatas.add(new BasicNameValuePair("id","8"));
     pairedDatas.add(new BasicNameValuePair("name","Customer Details"));
     pairedDatas.add(new BasicNameValuePair("datas",data);
6

There are 6 best solutions below

0
Viswa A On BEST ANSWER

There is no possible to pass arraylist values in basicnamevalue pair.the only thing we can do this.Convert the product array as jsonArray and later convert it as string and pass it as string in basicnamevaluepair.At server side we need to parse string using jsondecode whose key having jsonarray as string .

2
Sandeep P On

you cant add BasicNameValuePair

List<BasicNameValuePair> pairedData=new ArrayList<BasicNameValuepair();
pairedDatas.add(new BasicNameValuePair("id","8"));
pairedDatas.add(new BasicNameValuePair("name","Customer Details"));

 ArrayList<String> data=new ArrayList<String>();
 data.add("Datas");
 data.add("Datas2");
 data.add("Datas3");
// you need to convert tha array to formatted String 
JSONArray jsArray = new JSONArray(data);
pairedDatas.add(new BasicNameValuePair("datas",data.toString());
0
Mayank Sugandhi On

You have toi use jsonArray to send arraylist, use this code

JSONObject outerJsonObject ;
            JSONArray _mainArray ;
outerJsonObject = new JSONObject();
                _mainArray = new JSONArray();
    for (int i = 0; i < count; i++) {

                        JSONObject _jSubObj = new JSONObject();
                        try {

                            _jSubObj.put("user_id",_userId);
                            _jSubObj.put("first_name",name.get(i));

                                 }
                            _mainArray.put(_jSubObj);
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                 try {
                    outerJsonObject.put("address_details",_mainArray);


                    //                  System.out.println("OuterJSON request"+outerJsonObject);
                    //                  System.out.println("JSON ARRAY : " + _mainArray);
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

In this code name is arraylist at position of i.

and parameter use like

// Creating HTTP client
                    client = new DefaultHttpClient();

                    // Creating HTTP Post
                    HttpPost httpPost = new HttpPost(WebServiceConstants
                            .getMethodUrl(WebServiceConstants.METHOD_ADD_ADDRESS));

                    // Building post parameters, key and value pair
                    List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();

                    nameValuePair.add(new BasicNameValuePair("address_details", outerJsonObject.toString()));


                    System.out.println("Whole name value pair Address=" + nameValuePair.toString());

                    // Url Encoding the POST parameters
                    try {
                        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
                    } catch (UnsupportedEncodingException e) {
                        // writing error to Log
                        e.printStackTrace();
                    }

                    try {
                        responseMain = client.execute(httpPost);
                        //                      Log.e("RESPONSe of My order", String.valueOf(responseMain));
                        HttpEntity entitity = responseMain.getEntity();
                        _responseMain = EntityUtils.toString(entitity); // content will be consume only once
                        System.out.println("Response of Address "+_responseMain);
                    } catch (Exception e) 
                    {
                        e.printStackTrace();
                    }

I hope it is working

0
anu On

Download GSON library from hare http://www.java2s.com/Code/Jar/g/Downloadgson222jar.htm and add library in your project lib folder.

try this code

List<storeData> list=new ArrayList<storeData>();
for(int i=1;i<10;i++){
  storeData("0"+i);}
private void storeData(String s)
{

    storeData objDetails = new storeData(s);

    list.add(objDetails);

}

class storeData {

    private String strNumber;

    public storeData(String l) {
        strNumber = l; 
    }
}
Gson gson = new Gson();   
String jsonString = gson.toJson(list);  // convert data to json 

List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("jsonval", jsonString));  // pass data to server 

note : parse json data in server side and insert data into table . you can modify this code as per ur requirements .

0
Syed Danish Haider On

You can add array list to name value pair as mentioned in the code.Just convert array list to string by .toString() method. Initialise String builder first.

StringBuilder TaskID= new StringBuilder("");
StringBuilder FFCheckListID= new StringBuilder("");
StringBuilder FFCheckListLineItemSNo= new StringBuilder("");
TaskID.append("taskid");
 TaskID.append(",");
 FFCheckListID.append("id");
FFCheckListLineItemSNo.append("SNO");

AND ADD STRINGBUILDER TO ARRAYLIST

0
Saeed Parand On

I was faced with this kind of challenge , I solved my issue with this way.

LinkedList<String> llistobj = new LinkedList<String>();
ArrayList<String> arraylist= new ArrayList<String>();
arraylist.add("material_id");
arraylist.add("1");
arraylist.add("amount");
arraylist.add("10");
arraylist.add("material_id");
arraylist.add("2");
arraylist.add("amount");
arraylist.add("20"); 
ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(newBasicNameValuePair("materials_receive",
Arrays.toString(llistobj.toArray())));