Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 有人能解释一下这段代码吗?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
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);
       @L_616_0@ entity = response.getEntity();
       is = entity.getContent();

}catch(Exception E){
       Log.e("log_tag","Error in http connection "+e.toString());
}
}

我的问题……

BasicNameValuePair类有什么作用?

这条线是做什么的

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

是什么= entity.getContent();做?我可以在BasicNameValuePair类中传递多个值.我可以完全通过VO而不是这个.

像下面这样……

nameValuePairs.add(new BasicNameValuePair("year","1970","sas","saassa","asas"));

解决方法

BasicNameValuePair是一个对象,特别是一个容纳数据和键的容器.

例如,如果您有这些数据:

Name: Bob

Family name: Smith

Date of birth: 10/03/1977

然后你将这些数据存储为:

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

nameValuePairs.add(new BasicNameValuePair("name","bob"));

nameValuePairs.add(new BasicNameValuePair("family name","Smith"));

....

如您所见,您选择一个键(“名称”)和要存储为键的数据(“bob”).它是一种数据结构,用于加速并更容易存储此类信息.

另一方面,您需要一个工具来使用此数据:

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

这段代码可分为4部分:

httppost.setEntity

是一种将url作为参数的方法,并尝试使用http Post方法从该URL检索数据(HTML或存储在该页面上的内容).

新的UrlEncodedFormEntity

是一种在http服务器可以理解的内容中转换关键数据值对的方法.

它使用惯例

&key=input

一个最常用,但请记住有更多方法可以做到这一点.

nameValuePair

是您之前存储的数据.在这种情况下,它具有键入html中可能的输入形式,由“input name =”标记标识.作为数据,它具有您希望为表单提供的值.

is = entity.getContent();

@L_616_0@是一个帮助您处理可能结果的抽象.如果网站无法访问或连接断开,@L_616_0@将通知您. getContent()是你使用检索http结果体的方法,即:网络服务器发送给你的html,作为输入流.如果请求不成功,它将为您提供空值.

BasicNameValuePair只接受对联,所以你必须多次施放它,并且每次都将它@L_673_23@到arraylist.

您不能将其转换为两个以上的值,因为它们对于数据的(键,值)表示没有意义.

希望它有所帮助.

大佬总结

以上是大佬教程为你收集整理的android – 有人能解释一下这段代码吗?全部内容,希望文章能够帮你解决android – 有人能解释一下这段代码吗?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。