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:
/ / 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 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));
}
/ / 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));
}
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...
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...
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...
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...
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...
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...
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...
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...
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 Use the relative positioning to change the left value to achieve the carousel scrolling effect....