39

I want to know about What is the use of List<NameValuePair> or ArrayList<NameValuePair> in android? Specially when we are using web services using AsyncTask<...>

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Zubair Ahmed
  • 2,857
  • 2
  • 27
  • 47

5 Answers5

61

NameValuePair is a special <Key, Value> pair which is used to represent parameters in http request, i.e. www.example.com?key=value.

NameValuePair is an interface and is defined in apache http client, which is widely used in java to handle http operations. A List<NameValuePair> is just a list of <key, value> pairs, and will be used as params in http post request.

HttpPost request = new HttpPost();
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("key", "value"));
request.setEntity(new UrlEncodedFormEntity(params));

httpClient.execute(request);
Kenster
  • 23,465
  • 21
  • 80
  • 106
faylon
  • 7,360
  • 1
  • 30
  • 28
12

Some useful stuff for you.

List is an interface and ArrayList is an implementation of the List interface.

List: List is an interface in collection framework. several classes like ArrayList, LinkedList implement this interface. List is an ordered collection , so the position of an object does matter.

ArrayList: An ArrayList is a class which can grow at runtime. you can store java objects in an ArrayList and also add new objects at run time.

You will use ArrayList when you don't have to add or remove objects frequently from it. Because when you remove an object, all other objects need to be re-positioned inside the ArrayList, if you have this kind of situation, try using LinkedList instead.

You can find out more information from here.

Sabir Khan
  • 9,826
  • 7
  • 45
  • 98
Harshid
  • 5,701
  • 4
  • 37
  • 50
10

List<NameValuePair> or ArrayList<NameValuePair> are used to send values from android app to server.

@Override
    protected Header[] doInBackground(String... params) {
        try {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(params[1]);

            ArrayList<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
            nameValuePair.add(new BasicNameValuePair("ssn", params[0]));
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair,
                    "UTF-8"));
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            response = EntityUtils.toString(httpEntity);
            allHeaders = httpResponse.getAllHeaders();

        } catch (Exception ex) {

            ex.printStackTrace();

        }
        return allHeaders;
    }

In the code above, I'm passing an attribute ssn to server using ArrayList<NameValuePair>.

Anil M
  • 1,297
  • 2
  • 19
  • 42
0

Used this code for Upload Datas from android app to server side..

  // image file *********************************
 // here send all the sqlite database datas to local sever via HttpPost("url"); 

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

nameValuePairs.add(new BasicNameValuePair("job_id","001"));
nameValuePairs.add(new BasicNameValuePair("picture_path",picturejson.toString()));
        nameValuePairs.add(new BasicNameValuePair("date_time",datetime_str));
        nameValuePairs.add(new BasicNameValuePair("location",gpslocation_str));
        try{

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://Sample/iphone/getinput");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        httpclient.execute(httppost);
        HttpResponse response = httpclient.execute(httppost);
        //HttpEntity entity = response.getEntity();
        //is = entity.getContent();
        //HttpResponse response = httpClient.execute(postRequest);
         BufferedReader reader = new BufferedReader(new InputStreamReader(
             response.getEntity().getContent(), "UTF-8"));
                        String sResponse;
                        StringBuilder s = new StringBuilder();

                        while ((sResponse = reader.readLine()) != null) {
                            s = s.append(sResponse);
                        }
                    System.out.println("Response: ");
                    Log.v("hari", "Response : ");
        }catch(Exception e){
        //Log.e("log_tag", "Error in http connection "+e.toString());

        }
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
harikrishnan
  • 1,985
  • 4
  • 32
  • 63
0
protected String  doInBackground(String... params)
        {
            try
            {
                List<NameValuePair> param = new ArrayList<NameValuePair>();
            param.add(new BasicNameValuePair("username", edName));
            param.add(new BasicNameValuePair("email", edEmail));
            param.add(new BasicNameValuePair("password",  edPassword));
            param.add(new BasicNameValuePair("mobile", edMobile));


            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(params[0]);
            httpPost.setEntity(new UrlEncodedFormEntity(param));

            HttpResponse httpResponse = httpClient.execute(httpPost);

            HttpEntity httpEntity = httpResponse.getEntity();
            InputStream is = httpEntity.getContent();

            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;

            while ((line = reader.readLine()) != null)
            {sb.append(line + "\n");
            }

            String  json = sb.toString();
            JSONObject jObj = new JSONObject(json);
            msg= jObj.getString("message");
        }
        catch(Exception e)
        {

            Log.e("error", "Network problem");
        }
        return msg;

    }
}
Zubair Ahmed
  • 2,857
  • 2
  • 27
  • 47