谁能给我解释一下这个代码?[英] Can anyone explain me this code?

本文是小编为大家收集整理的关于谁能给我解释一下这个代码?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

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

我的问题...

basicnamevaluepair类是什么?

这条线路做了什么?

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

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

就像下面......

nameValuePairs.add(new BasicNameValuePair("year","1970","sas","saassa","sas","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:使用HTTP POST方法将URL作为参数作为参数拍摄的方法,并尝试从该URL检索数据(HTML或存储在该页面上的内容).

  • new UrlEncodedFormEntity:是在HTTP服务器可理解的内容中追溯关键数据值对的方法.

    它使用该惯例:&key=input,哪一个最使用的,但请记住更多的方法可以做到这一点.

  • nameValuePair:是您之前存储的数据.在这种情况下,它在HTML中具有键,由"input name="标签标识.作为数据,它有您想要给出表单的值.

  • is = entity.getContent();:HttpEntity是一种抽象,以帮助您处理可能的结果.如果网站无法访问或连接下来,HttpEntity会通知您. getContent()是您使用检索HTTP结果的主体的方法,即:Web服务器向您备份的HTML作为InputStream.如果请求没有成功,它将为您提供空值.

BasicNameValuePair只接受对联,因此您必须多次投用它,并且每次将其添加到ArrayList.

您无法将其投递到两个以上的值,因为它们对于(key, value)数据表示毫无意义.

希望它有帮助.

其他推荐答案

最终您正在使用该字段"年"具有"1970"的字段"年".<​​/p>

就像那个年份的WebForm发布一样.

有点额外: BasicNameValuePair看起来非常恰如其恰当地命名:它是一个非常简单的(基本)的两件事(一对),它用作Formfield(名称)及其内容(价值).

将年和1970年的组合添加到HttpPost对象,但是通过编码(所以没有"没有"非法的"事物).

本文地址:https://www.itbaoku.cn/post/102449.html

问题描述

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.

其他推荐答案

In the end you're doing a http POST request with the field "year" having the value "1970".

Just like a webform posting that year would.

A bit extra: The BasicNameValuePair looks quite aptly named: Its a very simple (basic) group of two things (pair) that serve as a formfield (name) and its contents (value).

The httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); adds that combination of year and 1970 to the HttpPost object, but with encoding (so there are no 'illegal' things in there).