HttpClient 使用简单案例(scala 版本)
1. 背景
1.1 概述
- 在常见的客户端服务器模型中,最开始是BS模型,也就是browser和server模型,因为最开始都是电脑,浏览器访问占据主流地位。阿里巴巴,京东都是先从网站做起的。
- 之后的模型变成了cs模型,client客户端,server服务器。这是由于互联网兴起,各种移动客户端流量开始占据主流,如手机,平板,甚至手表,汽车都可以访问网络。
- 在这个过程中,作为企业级开发语言主流的java,也出现了很多网络请求库,其中比较有历史的就是httpclient这个库。比较悠久,但目前实际在安卓开发中,早就已经由谷歌提出了新的网络库选择,在安卓6.0之后,谷歌移除了httpclient的api接口了。
- 本文只是处于怀旧情况,回忆一下httpclient的简单使用
- 实际安卓开发中,目前可以有
- HttpClient 这是底层的库之一,但目前谷歌官方不建议再在安卓开发中使用
- HttpURLConnection ,这是sun公司编写的底层网络库,api较少,谷歌建议使用这种底层请求库
- Volley,本身是基于底层请求库做了封装。在Android 2.3及以上的版本,使用的是HttpURLConnection,而在Android 2.2及以下版本,使用的是HttpClient。HurlStack和HttpClientStack分别对应HttpURLConnection 和HttpClient
- OkHttp,okhttp是square公司开发的网络框架。目前使用较多的网络库之一。Android 4.4后,HttpURLConnection底层实现使用的就是OkHttp
- Retrofit,Retrofit与okhttp一样出自于Square公司。Retrofit是一个封装的网络框架,底层网络框架使用的是OkHttp(Retrofit 2.0 开始不支持其他底层网络框架)。
1.2 官网介绍等
- http://hc.apache.org/httpclient-3.x/

- github
https://github.com/apache/httpcomponents-client

- maven
https://mvnrepository.com/search?q=httpclient


2. 代码
- 环境准备
- maven 3.6.3
- idea 2020
- jdk 1.8
- scala 2.12.12
- pom文件
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<scala.version>2.12.10</scala.version>
<spark.version>3.0.1</spark.version>
<hbase.version>2.2.5</hbase.version>
<hadoop.version>3.2.1</hadoop.version>
<encoding>UTF-8</encoding>
</properties>
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scala.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.12</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.12</artifactId>
<version>${spark.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.73</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<executions>
<execution>
<id>scala-compile-first</id>
<phase>process-resources</phase>
<goals>
<goal>add-source</goal>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>scala-test-compile</id>
<phase>process-test-resources</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
- 演示代码
package com.doit.day07
import com.alibaba.fastjson.{JSON, JSONObject}
import org.apache.http.HttpEntity
import org.apache.http.client.methods.{CloseableHttpResponse, HttpGet}
import org.apache.http.impl.client.{CloseableHttpClient, HttpClients}
import org.apache.http.util.EntityUtils
object HttpClientTest {
def main(args: Array[String]): Unit = {
val httpClient: CloseableHttpClient = HttpClients.createDefault()
val longitude = 119.3
val latitude = 40
val httpGet = new HttpGet(s"https://restapi.amap.com/v3/geocode/regeo?&location=$longitude,$latitude&key=4924f7ef5c86a278f5500851541cdcff")
val response: CloseableHttpResponse = httpClient.execute(httpGet)
val entity: HttpEntity = response.getEntity
if(response.getStatusLine.getStatusCode == 200) {
val resultStr: String = EntityUtils.toString(entity)
val jsonObj: JSONObject = JSON.parseObject(resultStr)
val regeocode: JSONObject = jsonObj.getJSONObject("regeocode")
if(regeocode != null && regeocode.isEmpty == false) {
val address: JSONObject = regeocode.getJSONObject("addressComponent")
println(address)
}
}
httpClient.close()
}
}