What is the use of List<NameValuePair> or ArrayList<NameValuePair>

佐手、 提交于 2019-12-28 03:39:08

问题


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<...>


回答1:


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



回答2:


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.




回答3:


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>.




回答4:


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

        }



回答5:


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;

    }
}


来源:https://stackoverflow.com/questions/17607589/what-is-the-use-of-listnamevaluepair-or-arraylistnamevaluepair

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!