我想知道什么是List<NameValuePair>
或ArrayList<NameValuePair>
在android中的用途?特别是当我们使用网络服务时AsyncTask<...>
5 回答
NameValuePair 是一个特殊的<Key, Value>
对,用于表示 http 请求中的参数,即www.example.com?key=value
.
NameValuePair 是一个接口,定义在 apache http 客户端中,在 java 中广泛用于处理 http 操作。AList<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
: AnArrayList
是一个可以在运行时增长的类。您可以将 java 对象存储在 an 中ArrayList
,还可以在运行时添加新对象。
ArrayList
当您不必经常从中添加或删除对象时,您将使用它。因为当你删除一个对象时,所有其他的对象都需要重新定位在 中ArrayList
,如果你有这种情况,请尝试使用LinkedList
。
您可以从这里了解更多信息。
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;
}
在上面的代码中,我ssn
使用ArrayList<NameValuePair>
.
使用此代码将数据从 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;
}
}