任何人都可以解释我这个code? [英] Can anyone explain me this code?

查看:158
本文介绍了任何人都可以解释我这个code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 进口org.apache.http.message.BasicNameValuePair;

私人字符串getServerData(字符串returnString){
InputStream的是= NULL;

字符串结果=;
//一年要发送的数据
ArrayList的<的NameValuePair> namevaluepairs中=新的ArrayList<的NameValuePair>();
nameValuePairs.add(新BasicNameValuePair(年,1970年));

// HTTP POST
尝试{
       HttpClient的HttpClient的=新DefaultHttpClient();
       HttpPost httppost =新HttpPost(KEY_121);
       httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));
       HTT presponse响应= httpclient.execute(httppost);
       HttpEntity实体= response.getEntity();
       是= entity.getContent();

}赶上(例外五){
       Log.e(log_tag,错误的HTTP连接+ e.toString());
}
}
 

我的问题...

这是什么BasicNameValuePair类呢?

这是什么片行做

  httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));
 

这是什么是= entity.getContent(); 吗?而我可以传递BasicNameValuePair类多个值。我完全可以通过这一个VO来代替。

像下面...

  nameValuePairs.add(新BasicNameValuePair(年,1970年,SAS,saassa,SAS,ASAS));
 

解决方案

BasicNameValuePair是一个对象,特别是一个容器来保存数据和密钥。

例如,如果你有这样的数据:

 名称:鲍勃

姓:史密斯

出生日期:1977年10月3日
 

那么你会存储这些数据为:

 的ArrayList<的NameValuePair> namevaluepairs中=新的ArrayList<的NameValuePair>();

nameValuePairs.add(新BasicNameValuePair(名,鲍勃));

nameValuePairs.add(新BasicNameValuePair(姓,史密斯));

....
 

正如你看你选择的关键(姓名)和数据存储为连接到钥匙(鲍勃)。它是一个类型的数据结构用于加快和使容易储存这种信息的

在另一端,你需要一个工具来使用这个数据:

  httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));
 

这code能在4个部位可分为:

  httppost.setEntity
 

是一种方法,需要一个URL作为参数,并试图检索数据(HTML或什么是存储在页)从URL,使用HTTP POST方法。

新UrlEn codedFormEntity

是trasform关键数据值对的东西理解HTTP服务器的方法。

它使用约定

 &放大器;键=输入
 

最常用的其中之一,但请记住,有更多的方式来做到这一点。

 的NameValuePair
 

是你之前存储的数据。在这种情况下,它具有键的可能的输入的形式在html,确定了输入名字=标记。由于数据有,你想给的表格中的值。

 是= entity.getContent();
 

HttpEntity是一个抽象的概念,以帮助您处理可能的结果。如果Web站点不可达或连接中断,HttpEntity会通知你。的getContent()是用于检索的HTTP结果体内,即法:即网络服务器发送你回来,作为一个InputStream中的HTML。如果要求没有succesfull它会给你一个空值。

BasicNameValuePair只接受春联,所以你必须要投它多次,每次将其添加到ArrayList

您不能将它转换为两个以上的价值,因为他们将是毫无意义的(键,值)再$ P $的数据psentation。

希望它帮助。

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());
}
}

My Questions...

What does BasicNameValuePair class does?

What does this piece of line do

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

What does is = entity.getContent(); do? and can i pass more than one value in BasicNameValuePair class. Can i entirely pass a VO instead of this.

Like the below ...

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

解决方案

BasicNameValuePair is an object, specifically a container to holds data and keys.

For example if you have this data:

Name: Bob

Family name: Smith

Date of birth: 10/03/1977

then you would store this data as:

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

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

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

....

As you see you choose a key ("name") and data to be stored as linked to the key ("bob"). It's a type of data structure used to speed up and make easier to store this kind of informations.

On the other end you need a tool to use this data:

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

this code can be divided in 4 parts:

httppost.setEntity

Is a method that take an url as argument, and tries to retrieve data (HTML or what is stored on that page) from that url, using the HTTP Post method.

new UrlEncodedFormEntity

Is a method that trasform key-data value pair in something intelligible by an http server.

it use the convention

&key=input

which one of the most used, but remember that there more ways to do it.

nameValuePair

is the data you stored before. In this case it has key the possible input forms in the html, identified by the "input name=" tag. As data it has the value that you want to give to the forms.

is = entity.getContent();

HttpEntity is an abstraction to help you handle the possible result. If the web site is unreachable or the connection is down, HttpEntity will inform you. getContent() is the method you use the retrieve the body of the Http result, i.e.: the html that the webserver sent you back, as a inputstream. If the request wasn't succesfull it will give you a null value.

BasicNameValuePair accept only couplets, so you'll have to cast it multiple times and everytime add it to the arraylist.

You can't cast it to more than two values, as they would be meaningless for the (key, value) representation of data.

Hope it helped.

这篇关于任何人都可以解释我这个code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆