Can someone explain this code to me?

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 the BasicNameValuePair class do?

What does this part of the line do

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

What does it do is = entity.getContent();? and I can pass more than one value in the BasicNameValuePair class. Can I completely transfer VO instead.

As below ...

nameValuePairs.add(new BasicNameValuePair("year","1970","sas","saassa","sas","asas"));
+3
source share
2 answers

BasicNameValuePair is an object, in particular a container for storing data and keys.

For example, if you have this data:

Name: Bob

Family name: Smith

Date of birth: 10/03/1977

then you save this data as:

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

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

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

....

As you can see, you select the key ("name") and the data that should be stored as key-bound ("bob"). This is a type of data structure used to speed up and simplify the storage of this kind of information.

:

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

4 :

httppost.setEntity

, URL- (HTML , ) URL-, HTTP Post.

new UrlEncodedFormEntity

, -, HTTP-.

&key=input

, , .

nameValuePair

- , . html, "input name=". , .

is = entity.getContent();

HttpEntity - , . - , HttpEntity . getContent() - , Http, ..: html, - . , .

BasicNameValuePair , arraylist.

, (, ) .

, .

+17

http POST "", "1970".

, - .

: BasicNameValuePair : () (), () ().

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 1970 HttpPost, ( "" ).

+4

Source: https://habr.com/ru/post/1787652/


All Articles