1. The client an HTTP connection consists of two directions, one for this HTTP connection, and the other for the HTTP connection output.
After the IP address and port number are connected using httpclient, the HTTP output is set as the HTTP request parameter. The HTTP output is used for HTTP request settings.
The HTTP input is used to receive data sent back from the server.
There is a key http.openconencetion () method to initiate the connection. and Httpconn.getinputstream () are used to receive data returned by the server side.
1, the client gets the JSON string Public classhttputil{ Public Staticstring Getjsoncontent (String urlstr) {Try {//Get httpurlconnection Connection objectURL url =NewURL (URLSTR); HttpURLConnection Httpconn=(httpurlconnection) URL. OpenConnection (); //Setting Connection PropertiesHttpconn.setconnecttimeout ( the); Httpconn.setdoinput (true); Httpconn.setrequestmethod ("GET"); //get the corresponding code intRespcode =Httpconn.getresponsecode (); if(Respcode = = $) { returnConvertstream2json (Httpconn.getinputstream ()); } } Catch(malformedurlexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } return ""; } Private Staticstring Convertstream2json (InputStream inputstream) {string Jsonstr=""; //bytearrayoutputstream equivalent to memory output streamBytearrayoutputstream out=NewBytearrayoutputstream (); byte[] buffer =New byte[1024x768]; intLen =0; //transfer the input stream to the memory output stream Try { while(len = inputstream.read (buffer,0, buffer.length))! =-1) { out. write (buffer,0, Len); } //converting a memory stream to a stringJsonstr =NewString ( out. Tobytearray ()); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } returnJsonstr; }}
The client an HTTP connection consists of two directions, one for this HTTP connection, and the other for the HTTP connection output.