我想了解在Android中使用
List<NameValuePair>
或ArrayList<NameValuePair>
的作用是什么?特别是在使用AsyncTask<...>
调用Web服务时。List<NameValuePair>
或ArrayList<NameValuePair>
的作用是什么?特别是在使用AsyncTask<...>
调用Web服务时。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);
这里有些对你有用的内容。
List
是一个接口,ArrayList
是实现了List
接口的类。
List
: List
是集合框架中的一个接口,一些类如ArrayList
和LinkedList
实现了该接口。 List
是一个有序的集合,因此对象的位置很重要。
ArrayList
:一个ArrayList
是一个可以在运行时增长的类。你可以在ArrayList
中存储Java对象,也可以在运行时添加新的对象。
当你不需要经常添加或删除对象时,会使用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;
}
在上面的代码中,我使用 ArrayList<NameValuePair>
将属性 ssn
传递给服务器。使用此代码将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;
}
}