2013-07-12 107 views

回答

51

的NameValuePair是一個特殊的<Key, Value>一對用於表示在http請求參數,即www.example.com?key=value

NameValuePair是一個接口,並在apache http客戶端中定義,它在java中被廣泛用於處理http操作。 A 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); 
+0

代碼行3應該是params.add(new BasicNameValuePair(「key」,「value」));缺少) – pahan

+0

我可以在HTTP請求以外的其他東西中使用它嗎? –

12

一些有用的東西給你。

List是一個接口,ArrayListList接口的實現。

ListList是集合框架中的一個接口。幾個類如ArrayList,LinkedList實現這個接口。 List是一個有序的集合,所以對象的位置確實很重要。

ArrayListArrayList是一個可以在運行時增長的類。您可以將java對象存儲在ArrayList中,並且還可以在運行時添加新對象。

當您不必頻繁地添加或刪除對象時,您將使用ArrayList。因爲當你移除一個物體時,所有其他物體需要重新放置在ArrayList之內,如果你有這種情況,請嘗試使用LinkedList代替。

你可以從here找到更多的信息。

+0

如果這有助於你再接受這個答案! – Harshid

8

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

接受答案,如果它的幫助! –

0

用於上傳DATAS這個代碼從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; 

    } 
} 
相關問題