我想知道在android中有什麼用List<NameValuePair>
或ArrayList<NameValuePair>
?特別是當我們使用網絡服務AsyncTask<...>
有什麼用的List <NameValuePair>或ArrayList <NameValuePair>
回答
的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);
一些有用的東西給你。
List
是一個接口,ArrayList
是List
接口的實現。
List
:List
是集合框架中的一個接口。幾個類如ArrayList
,LinkedList
實現這個接口。 List
是一個有序的集合,所以對象的位置確實很重要。
ArrayList
:ArrayList
是一個可以在運行時增長的類。您可以將java對象存儲在ArrayList
中,並且還可以在運行時添加新對象。
當您不必頻繁地添加或刪除對象時,您將使用ArrayList
。因爲當你移除一個物體時,所有其他物體需要重新放置在ArrayList
之內,如果你有這種情況,請嘗試使用LinkedList
代替。
你可以從here找到更多的信息。
如果這有助於你再接受這個答案! – Harshid
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
到服務器。
接受答案,如果它的幫助! –
用於上傳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());
}
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;
}
}
- 1. 爲什麼我們寫ArrayList <ArrayList <Integer>> list = new ArrayList <>();
- 2. ArrayList <?>,ArrayList,ArrayList <Object>有什麼區別?
- 3. 列表<String> list = new ArrayList <String>和ArrayList <String> list = new ArrayList <String>之間有什麼不同?
- 4. List <Something>和List <?有什麼區別?擴展Something>?
- 5. List <Object> listObject = new ArrayList <Object>()?
- 6. ArrayList <Integer> a [];有什麼區別?和ArrayList <Integer> a ;?
- 7. 用C#中的List <>替換ArrayList
- 8. List <>優化,有什麼可能?
- 9. <?或<?php ---有什麼區別?
- 10. 爲什麼是List <>。OrderBy LINQ比IComparable + List <>更快。
- 11. 如何將ArrayList <Object>轉換爲ArrayList <String>或ArrayList <Timestamp>?
- 12. HashSet <T>和List <T>有什麼區別?
- 13. 使用從ArrayList或List繼承的索引器VS <T>?
- 14. List l = new ArrayList <Number>(); l的靜態類型是List <Number>?那是什麼意思?
- 15. ArrayList的用法<ArrayList<E>>
- 16. Arraylist <Interger> vs int [] list = new int [3];
- 17. 新的ArrayList <Class>()或新的ArrayList <>()在Java中?
- 18. 什麼是「列表<Integer> list = new ArrayList <Integer>();」其實是什麼意思?
- 19. List <JAXBElement <?是什麼?擴展SomeClassName >>的意思是?
- 20. 將List <MyObject>轉換爲List <IMyInterface>的最佳方式是什麼?
- 21. ArrayList <Object <ArrayList <Object> >>
- 22. public <T extends Animal> void addAll(List <T> animals)and public void addAll(List <Animal> animals)有什麼區別?
- 23. List <BusinessObject>或BusinessObjectCollection?
- 24. 多態性:爲什麼使用「List list = new ArrayList」而不是「ArrayList list = new ArrayList」?
- 25. <?php,<?, <?=,有什麼區別?
- 26. MalformedParameterizedTypeException當使用ArrayList <ArrayList<>>
- 27. ArrayList <ArrayList <String>> vs ArrayList <Object>
- 28. 在html中有什麼用<head>或<html>標籤?
- 29. 如何將List <Bear>或List <Goat>分配到列表<IAnimal>
- 30. 按鍵排序映射<ArrayList,List <Entity>> - ArrayList設置日期
代碼行3應該是params.add(new BasicNameValuePair(「key」,「value」));缺少) – pahan
我可以在HTTP請求以外的其他東西中使用它嗎? –