HttpClient access network, httpclient access

Source: Internet
Author: User

HttpClient access network, httpclient access

In the HttpClient project, Apache provides classes for accessing the network and encapsulates the methods for accessing the network. The input and output operations in the HttpURlConnection class are encapsulated into HttpGet, HttpPost, and HttpResponse

I. Server front-end

1. Steps for sending a GET request:

(1) Create an HttpClient object: HttpClient httpClient = new DefaultHttpClient ();

(2) create HttpGet object: HttpGet httpGet = new HttpGet (http://www.baidu.com );

(3) Add the sending parameter httpGet. setParams ("the girl's eyes are really big, so pretty! ");

(4) Call the execute () method of the HttpClient object to send the request:

HttpResponse httpResponse=httpClient.exe cute ();

Example:

HttpClient httpClient = new DefaultHttpClient ();

HttpGet httpGet = new HttpGet (http://www.iciba.com );

HttpGet. setParams ("awesome! ");

Tyr {

HttpResponse httpResponse=httpClient.exe cute ();

// Determine whether the server responds

If (httpResponse. getStatusLine (). getStatusCode () = HttpStatus. SC _ OK ){

Result = EntityUtils. toString (httpResponse. getEntiry (); // obtain the returned string

} Else {result = "request failed! ";}

} Catch (ClientProtocolException e ){

E. printStackTrace ();

} Catch (IOException e) {e. printStackTrace ();};

 


Android uses HttpClient to access the network server and the client.

Answer: The preliminary judgment is that there is a problem with json parsing. In general, you must specify the content of the json string to parse json. In layman's terms, you need to know the structure of the entire json string. There are several braces {}, including nested braces {} and several braces [], including nested []. Then, the outer and inner layers must be braces {}, which is a json object, that is, JsonObject. Then, at this layer, check whether there are any brackets []. the json array is JsonArray. The Field Values of the current layer can be obtained based on the json object and field names of the current layer. In this case, move down a layer of json object, that is, view the second braces {}, and so on.

I am a beginner. After I log on to httpClient, I want to access the specified webpage. What should I do? Teach me,

Official webpage examples and quickstart

Https: Colon // hc: apache: org/httpcomponents-client-ga/index.html
Replace the Chinese characters

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.