2

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);
Viswa A
  • 51
  • 1
  • 6
  • You can use JsonArray. – NehaK Jun 22 '15 at 11:07
  • Actually am using content type as application/x-www-form-urlencoded.How can change the json into this form – Viswa A Jun 22 '15 at 11:10
  • it depends on server side ... PHP for example can take somthing like datas[0]=Datas&datas[1]=Datas2 ... AFAIK `x-www-form-urlencoded` do not support arrays as is ... **edit:** of course you can use the json solutions bellow ... but ... then it will be better to send whole data as json ... – Selvin Jun 22 '15 at 11:23

6 Answers6

0

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());
Sandeep P
  • 4,291
  • 2
  • 26
  • 45
  • this is very old post, which android version u are using, this will work only for HTTP request, if you are using URL open connection u need changes – Sandeep P Oct 12 '16 at 09:23
  • Android 6.0, i have facing same issue, please help me. org.json.JSONException: End of input at character 0 of will occurred. – MohanRaj S Oct 12 '16 at 09:34
0

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 .

Viswa A
  • 51
  • 1
  • 6
0

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 .

anu
  • 213
  • 1
  • 3
  • 10
0

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

Syed Danish Haider
  • 1,334
  • 11
  • 15
0

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())));
Saeed Parand
  • 33
  • 1
  • 1
  • 9
-1

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

Mayank Sugandhi
  • 397
  • 1
  • 7
  • 22