android - Can anyone explain me this code? -
import org.apache.http.message.basicnamevaluepair; private string getserverdata(string returnstring) { inputstream = null; string result = ""; //the year data 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(); = entity.getcontent(); }catch(exception e){ log.e("log_tag", "error in http connection "+e.tostring()); } }
my questions...
what basicnamevaluepair class does?
what piece of line
httppost.setentity(new urlencodedformentity(namevaluepairs));
what is = entity.getcontent();
do? , can pass more 1 value in basicnamevaluepair class. can entirely pass vo instead of this.
like below ...
namevaluepairs.add(new basicnamevaluepair("year","1970","sas","saassa","sas","asas"));
basicnamevaluepair object, container holds data , keys.
for example if have data:
name: bob family name: smith date of birth: 10/03/1977
then store data as:
arraylist<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(); namevaluepairs.add(new basicnamevaluepair("name","bob")); namevaluepairs.add(new basicnamevaluepair("family name","smith")); ....
as see choose key ("name") , data stored linked key ("bob"). it's type of data structure used speed , make easier store kind of informations.
on other end need tool use data:
httppost.setentity(new urlencodedformentity(namevaluepairs));
this code can divided in 4 parts:
httppost.setentity
is method take url argument, , tries retrieve data (html or stored on page) url, using http post method.
new urlencodedformentity
is method trasform key-data value pair in intelligible http server.
it use convention
&key=input
which 1 of used, remember there more ways it.
namevaluepair
is data stored before. in case has key possible input forms in html, identified "input name=" tag. data has value want give forms.
is = entity.getcontent();
httpentity abstraction handle possible result. if web site unreachable or connection down, httpentity inform you. getcontent() method use retrieve body of http result, i.e.: html webserver sent back, inputstream. if request wasn't succesfull give null value.
basicnamevaluepair accept couplets, you'll have cast multiple times , everytime add arraylist.
you can't cast more 2 values, meaningless (key, value) representation of data.
hope helped.
Comments
Post a Comment