List<NameValuePair>或ArrayList<NameValuePair>有什么用途?

39
我想了解在Android中使用 List<NameValuePair>ArrayList<NameValuePair>的作用是什么?特别是在使用AsyncTask<...>调用Web服务时。
5个回答

60

NameValuePair是一种特殊的<Key, Value>对,用于表示HTTP请求中的参数,例如www.example.com?key=value

NameValuePair是一个接口,在apache http客户端中定义,它被广泛用于java处理http操作。一个List<NameValuePair>只是一组<key, value>对的列表,将被用作HTTP POST请求的参数。

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

httpClient.execute(request);

代码的第三行应该是:params.add(new BasicNameValuePair("key", "value")); 缺少 )。 - pahan
除了 HTTP 请求,我可以在其他地方使用它吗? - Francisco Corrales Morales

12

这里有些对你有用的内容。

List是一个接口,ArrayList是实现了List接口的类。

ListList是集合框架中的一个接口,一些类如ArrayListLinkedList实现了该接口。 List是一个有序的集合,因此对象的位置很重要。

ArrayList:一个ArrayList是一个可以在运行时增长的类。你可以在ArrayList中存储Java对象,也可以在运行时添加新的对象。

当你不需要经常添加或删除对象时,会使用ArrayList。因为当你删除一个对象时,所有其他对象都需要在ArrayList中重新定位,如果你遇到这种情况,请尝试使用LinkedList

你可以从这里找到更多信息。


10

List<NameValuePair>ArrayList<NameValuePair>用于从Android应用程序向服务器发送值。

@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;
    }
在上面的代码中,我使用 ArrayList<NameValuePair> 将属性 ssn 传递给服务器。

0

使用此代码将Android应用程序中的数据上传到服务器端。

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

        }

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;

    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接