【问题标题】:What is the use of List<NameValuePair> or ArrayList<NameValuePair>List<NameValuePair> 或 ArrayList<NameValuePair> 有什么用
【发布时间】:2013-07-12 04:44:35
【问题描述】:

我想知道List&lt;NameValuePair&gt;或ArrayList&lt;NameValuePair&gt;在android中有什么用?特别是当我们使用 AsyncTask&lt;...&gt; 使用 Web 服务时

【问题讨论】:

    标签: android android-asynctask


    【解决方案1】:

    NameValuePair 是一个特殊的&lt;Key, Value&gt; 对,用于表示http 请求中的参数,即www.example.com?key=value。

    NameValuePair是一个接口,定义在apache http客户端中,在java中广泛用于处理http操作。 List&lt;NameValuePair&gt; 只是 &lt;key, value&gt; 对的列表,将用作 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);
    

    【讨论】:

    • 代码行 3 应该是 params.add(new BasicNameValuePair("key", "value"));不见了)
    • 我可以在 HTTP 请求之外使用它吗?
    【解决方案2】:

    一些对你有用的东西。

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

    List:List是集合框架中的一个接口。像ArrayList、LinkedList 这样的几个类实现了这个接口。 List 是一个有序集合,所以对象的位置很重要。

    ArrayList:ArrayList 是一个可以在运行时增长的类。您可以将 java 对象存储在 ArrayList 中,还可以在运行时添加新对象。

    当您不必频繁添加或删除对象时,您将使用ArrayList。因为当你移除一个对象时,所有其他对象都需要在ArrayList内重新定位,如果你有这种情况,请尝试改用LinkedList。

    您可以通过here了解更多信息。

    【讨论】:

      【解决方案3】:

      List&lt;NameValuePair&gt; 或 ArrayList&lt;NameValuePair&gt; 用于将值从 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&lt;NameValuePair&gt; 将属性ssn 传递给服务器。

      【讨论】:

      • 如果有帮助请采纳!!
      【解决方案4】:

      使用此代码将数据从 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());
      
              }
      

      【讨论】:

        【解决方案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;
        
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2013-08-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-08-30
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多