import org.apache.http.message.BasicNameValuePair;
private String getServerData(String returnString) {
InputStream is = null;
String result = "";
//the year data to send
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("year","1970"));
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(KEY_121);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题......
BasicNameValuePair类有什么作用?
这条线是做什么的
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
Run Code Online (Sandbox Code Playgroud)
怎么is = entity.getContent();办?我可以在BasicNameValuePair类中传递多个值.我可以完全通过VO而不是这个.
像下面这样......
nameValuePairs.add(new BasicNameValuePair("year","1970","sas","saassa","sas","asas"));
Run Code Online (Sandbox Code Playgroud)
Mas*_*one 17
BasicNameValuePair是一个对象,特别是一个容纳数据和键的容器.
例如,如果您有这些数据:
Name: Bob
Family name: Smith
Date of birth: 10/03/1977
Run Code Online (Sandbox Code Playgroud)
然后你将这些数据存储为:
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("name","bob"));
nameValuePairs.add(new BasicNameValuePair("family name","Smith"));
....
Run Code Online (Sandbox Code Playgroud)
如您所见,您选择一个键("名称")和要存储为键的数据("bob").它是一种数据结构,用于加速并更容易存储此类信息.
另一方面,您需要一个工具来使用此数据:
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
Run Code Online (Sandbox Code Playgroud)
这段代码可分为4部分:
httppost.setEntity
Run Code Online (Sandbox Code Playgroud)
是一种将url作为参数的方法,并尝试使用HTTP Post方法从该URL检索数据(HTML或存储在该页面上的内容).
新的UrlEncodedFormEntity
是一种在http服务器可以理解的内容中转换关键数据值对的方法.
它使用惯例
&key=input
Run Code Online (Sandbox Code Playgroud)
哪一个最常用,但请记住有更多方法可以做到这一点.
nameValuePair
Run Code Online (Sandbox Code Playgroud)
是您之前存储的数据.在这种情况下,它具有键入html中可能的输入形式,由"input name ="标记标识.作为数据,它具有您希望为表单提供的值.
is = entity.getContent();
Run Code Online (Sandbox Code Playgroud)
HttpEntity是一个帮助您处理可能结果的抽象.如果网站无法访问或连接断开,HttpEntity将通知您.getContent()是您使用检索Http结果体的方法,即:网络服务器发回的html,作为输入流.如果请求不成功,它将为您提供空值.
BasicNameValuePair只接受对联,所以你必须多次施放它,并且每次都将它添加到arraylist.
您不能将其转换为两个以上的值,因为它们对于数据的(键,值)表示没有意义.
希望它有所帮助.
| 归档时间: |
|
| 查看次数: |
6799 次 |
| 最近记录: |