##### HttpClient

HttpClient

Android 6.0 release removes support for HttpClient client

Google recommends using HttpURLConnection. This API is more efficient, reducing network usage and reducing power consumption through transparent compression and response caching

To be added notes

To inherit from using HttpClient you need to declare it in build.gradle:

android {
  ...
  //Declaration
  useLibrary 'org.apache.http.legacy'
  ...
}

Add dependencies:

dependencies {
     / / Depend on HttpClient
  implementation 'org.apache.httpcomponents:httpclient-android:4.3.5.1'
}

Basic use:

  1. POST
/ / Create HttpClient
1HttpClient client = HttpClients.createDefault();

 / / Create a post request
HttpPost post = new HttpPost(url);

 // build request parameters
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("tel", phoneNum));

 / / Set the post request body
post.setEntity(new UrlEncodedFormEntity(params));

 //Execute the request
HttpResponse response = client.execute(post);

 / / Get results
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200) {
  String result = EntityUtils.toString(response.getEntity());
     / / Process the request result
}
  1. GET
//1 Create HttpClient
HttpClient client = HttpClients.createDefault();

 / / Create a get request: parameters spliced ​​in the url
HttpGet get = new HttpGet(url + "?tel=" + phoneNum);

 //Execute the request
HttpResponse response = client.execute(get);

 / / Get results
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200) {
  String result = EntityUtils.toString(response.getEntity());
  mHandler.sendMessage(mHandler.obtainMessage(UPDATE_UI, result));
}
  1. RequestBuild build request object
/ / Request configuration: connection timed out
RequestConfig requestConfig = RequestConfig
  .custom()
  .setConnectTimeout(5000)
  .build();
  
 / / Create httpclient
//HttpClient client = HttpClients.createDefault();
CloseableHttpClient client = HttpClients
  .custom()
  .setDefaultRequestConfig(requestConfig)
  .build();

 / / Build request object
HttpUriRequest request = RequestBuilder
     / / Request method
  .get()
  //.post()
     / / Request url
  .setUri(url)
     / / Request parameters
  .addParameter("tel", phoneNum)
  .build();

 / / Execute a request
HttpResponse response = client.execute(request);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200) {
  String result = EntityUtils.toString(response.getEntity());
  mHandler.sendMessage(mHandler.obtainMessage(UPDATE_UI, result));
}

Intelligent Recommendation

The configuration system of .NET COR

First of all, the configuration system ofnetcore is currently the most commonly -based configuration, such as JSON, XML files, and of course there are other commandline, EnvironmentVariables. 1. First...

SpringBoot uses AOP+Redis to implement a simple token login verification example

Create a SpringBoot project and introduce dependencies The idea is this: 1. When the user logs in successfully, a Token is randomly created, and then stored in the session and Redis respectively 2. Co...

LintCode 1005. JavaScript algorithm for the maximum area of ​​a triangle

description There are a series of points on the plane. Returns the largest area of ​​a triangle that can be formed by three points. Description 3 <= points.length <= 50. The points will not be r...

Web framework Django learning record (2) show the contents of the database in the page on the page

1. Create a project, apply 2. Modify configuration Modify YU1 \ YU1 \ Settings.py file installed_apps, add a line 'yuapp', templates, modify 'DIRS': [Base_Dir + "/ Templates",], 3. Modify YU...

In-depth analysis of HashSet and HashMap source code

HashSet source code analysis: Let’s take a look at its construction method: Uh~~ Its underlying layer is actually implemented using HashMap, subverting the three views. So how is it used? Contin...

More Recommendation

ZooKeeper Register Service Information --- Get IP Address and Idle Port (NodeJS TypeScript)

This object is column to write the 2017-19 update repair multi-NIC to get IP issues. Function call getIdleport (Callback: (portback: (port: Number, IP ?: string) => void)Winning this airline port i...

[Interview Question 04 10] Check the subtree

topic Topic link Check the subtree. You have two very large binary trees: T1, with tens of thousands of nodes; T2, with tens of thousands of nodes. Design an algorithm to determine whether T2 is a sub...

Python insertion sort

Introduction The working principle of insertion sort is that for each unsorted data, insert it into the previously sorted data, and compare the insertion from back to front step: Starting from the fir...

Android uses PopupWindow to implement the reuse class of pop-up warning boxes

Please indicate the source for reprinting: In Android development, I believe that everyone is familiar with the interface shown in the figure below, and the frequency of use of this pop-up box is also...

JS carousel map - seamless scrolling

JS carousel map – seamless scrolling Use the relative positioning to change the left value to achieve the carousel scrolling effect....

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top