HttpURLConnection访问HTTP接口的注意点:(1)HttpURLConnection默认采取UTF-8编码,但服务器可能返回GBK编码的报文;(2)有时服务器会先压缩应答报文,再把压缩后的数据送给调用方;(3)服务器返回报文超长的时候,不要企图一次性把返回数据读到某个字节数组,而要循环读取输入流中的字节数据,直到确定读完了全部的应答数据;
==============================================================================================================
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.myapplication">
<!-- 互联网 -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- 定位 -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!-- 存储卡读写 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<!-- 下载时不提示通知栏 -->
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
<!-- 相机 -->
<uses-permission android:name="android.permission.CAMERA" />
<!-- 录音 -->
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<!-- 存储卡读写 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication"
android:usesCleartextTraffic="true"
tools:targetApi="31">
<activity
android:name=".MainActivity7"
android:exported="false" />
<activity
android:name=".MainActivity6"
android:exported="false" />
<activity
android:name=".MainActivity5"
android:exported="false"
android:label="@string/title_activity_main5" />
<activity
android:name=".MainActivity4"
android:exported="false"
android:label="@string/title_activity_main4" />
<activity
android:name=".MainActivity3"
android:exported="false"
android:label="@string/title_activity_main3" />
<activity
android:name=".MainActivity2"
android:exported="false"
android:label="@string/title_activity_main2" />
<activity
android:name=".MainActivity"
android:exported="true">
<!-- android:theme="@style/AppCompatTheme" /> -->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp" >
<TextView
android:id="@+id/tv_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="暂未获取到定位对象"
android:textColor="@color/black"
android:textSize="17sp" />
</LinearLayout>
主代码:
package com.example.myapplication;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import com.example.myapplication.task.GetAddressTask;
import com.example.myapplication.util.DateUtil;
import com.example.myapplication.util.SwitchUtil;
@SuppressLint("DefaultLocale")
public class MainActivity extends AppCompatActivity implements GetAddressTask.OnAddressListener
{
private final static String TAG = "HttpGetActivity";
private TextView tv_location;
private Location mLocation; // 定位信息
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_location = findViewById(R.id.tv_location);
SwitchUtil.checkGpsIsOpen(this, "需要打开定位功能才能查看定位结果信息"); // 检查定位功能是否打开,若未打开则跳到系统的定位功能设置页面
}
@Override
protected void onResume()
{
super.onResume();
// 检查当前设备是否已经开启了定位功能
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
{
Toast.makeText(this, "请授予定位权限并开启定位功能", Toast.LENGTH_SHORT).show();
return;
}
// 从系统服务中获取定位管理器
LocationManager mgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// 获取最后一次成功定位的位置信息(network表示网络定位方式)
Location location = mgr.getLastKnownLocation("network");
getLocationText(location); // 获取定位结果文本
// 获取最后一次成功定位的位置信息(gps表示卫星定位方式)
location = mgr.getLastKnownLocation("gps");
getLocationText(location); // 获取定位结果文本
}
// 获取定位结果文本
private void getLocationText(Location location)
{
if (location != null)
{
mLocation = location;
refreshLocationInfo(""); // 刷新定位信息
GetAddressTask task = new GetAddressTask(); // 创建一个详细地址查询的异步任务
task.setOnAddressListener(this); // 设置详细地址查询的监听器
task.execute(location); // 把详细地址查询任务加入到处理队列
}
}
// 在找到详细地址后触发
@Override
public void onFindAddress(String address)
{
refreshLocationInfo(address); // 刷新定位信息
}
// 刷新定位信息
private void refreshLocationInfo(String address)
{
String desc = String.format("定位类型=%s\n定位对象信息如下: " + "\n\t其中时间:%s" + "\n\t其中经度:%f,纬度:%f" + "\n\t其中高度:%d米,精度:%d米" + "\n\t其中地址:%s",
mLocation.getProvider(), DateUtil.formatDate(mLocation.getTime()),
mLocation.getLongitude(), mLocation.getLatitude(),
Math.round(mLocation.getAltitude()), Math.round(mLocation.getAccuracy()), address);
tv_location.setText(desc);
}
}
DateUtil
package com.example.myapplication.util;
import android.annotation.SuppressLint;
import android.text.TextUtils;
import java.text.SimpleDateFormat;
import java.util.Date;
@SuppressLint("SimpleDateFormat")
public class DateUtil {
// 获取当前的日期时间
public static String getNowDateTime() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
return sdf.format(new Date());
}
// 获取当前的时间
public static String getNowTime() {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
return sdf.format(new Date());
}
// 获取当前的时间(精确到毫秒)
public static String getNowTimeDetail() {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");
return sdf.format(new Date());
}
// 获取指定格式的日期时间
public static String getNowDateTime(String formatStr) {
String format = formatStr;
if (TextUtils.isEmpty(format)) {
format = "yyyyMMddHHmmss";
}
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(new Date());
}
public static String formatDate(long time) {
Date date = new Date(time);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(date);
}
// 重新格式化日期字符串
public static String convertDateString(String strTime) {
String newTime = strTime;
try {
SimpleDateFormat oldFormat = new SimpleDateFormat("yyyyMMddHHmmss");
Date date = oldFormat.parse(strTime);
SimpleDateFormat newFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
newTime = newFormat.format(date);
} catch (Exception e) {
e.printStackTrace();
}
return newTime;
}
}
SwitchUtil
package com.example.myapplication.util;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.net.wifi.WifiManager;
import android.provider.Settings;
import android.widget.Toast;
public class SwitchUtil
{
// 获取定位功能的开关状态
public static boolean getGpsStatus(Context ctx)
{
// 从系统服务中获取定位管理器
LocationManager lm = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
return lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
// 检查定位功能是否打开,若未打开则跳到系统的定位功能设置页面
public static void checkGpsIsOpen(Context ctx, String hint)
{
if (!getGpsStatus(ctx))
{
Toast.makeText(ctx, hint, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
ctx.startActivity(intent);
}
}
}
GetAddressTask
package com.example.myapplication.task;
import android.location.Location;
import android.os.AsyncTask;
import android.text.TextUtils;
import android.util.Log;
import com.example.myapplication.util.HttpUtil;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
// 根据经纬度获取详细地址的异步任务
public class GetAddressTask extends AsyncTask<Location, Void, String> {
private final static String TAG = "GetAddressTask";
private String mQueryUrl = "https://api.tianditu.gov.cn/geocoder?postStr=%s&type=geocode&tk=253b3bd69713d4bdfdc116255f379841";
// 线程正在后台处理
protected String doInBackground(Location... params) {
Location location = params[0];
//String url = String.format(mQueryUrl, mLocation.getLongitude(), mLocation.getLatitude());
String param = String.format("{'lon':%f,'lat':%f,'ver':1}", location.getLongitude(), location.getLatitude());
try {
param = URLEncoder.encode(param, "utf8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String url = String.format(mQueryUrl, param);
// // 把经度和纬度代入到URL地址。天地图的地址查询url在UrlConstant.java中定义
// String url = String.format(UrlConstant.GET_ADDRESS_URL,
// location.getLongitude(), location.getLatitude());
Log.d(TAG, "url = " + url);
String resp = HttpUtil.get(url, null); // 发送HTTP请求信息,并获得HTTP应答内容
Log.d(TAG, "resp = " + resp);
String address = "未知";
// 下面从JSON串中解析formatted_address字段获得详细地址描述
if (!TextUtils.isEmpty(resp))
{
try
{
JSONObject obj = new JSONObject(resp);
JSONObject result = obj.getJSONObject("result");
address = result.getString("formatted_address");
}
catch (JSONException e)
{
e.printStackTrace();
}
}
Log.d(TAG, "address = " + address);
return address; // 返回HTTP应答内容中的详细地址
}
// 线程已经完成处理
protected void onPostExecute(String address)
{
mListener.onFindAddress(address); // HTTP调用完毕,触发监听器的找到地址事件
}
private OnAddressListener mListener; // 声明一个查询详细地址的监听器对象
// 设置查询详细地址的监听器
public void setOnAddressListener(OnAddressListener listener)
{
mListener = listener;
}
// 定义一个查询详细地址的监听器接口
public interface OnAddressListener
{
void onFindAddress(String address);
}
}
HttpUtil
package com.example.myapplication.util;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.util.Map;
import java.util.zip.GZIPInputStream;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
public class HttpUtil {
private final static String TAG = "HttpUtil";
private final static int CONNECT_TIMEOUT = 15000;
private final static int READ_TIMEOUT = 15000;
// 兼容https开头的调用地址
private static void compatibleSSL(String callUrl) throws Exception {
if (callUrl.toLowerCase().startsWith("https")) {
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, new TrustManager[]{new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkServerTrusted(X509Certificate[] arg0, String arg1) {
}
@Override
public void checkClientTrusted(X509Certificate[] arg0, String arg1) {
}
}}, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
}
}
// 对指定接口地址发起GET调用
public static String get(String callUrl, Map<String, String> headers)
{
String resp = ""; // 应答内容
try
{
Log.d(TAG, "请求地址:"+callUrl);
compatibleSSL(callUrl); // 兼容https开头的调用地址
URL url = new URL(callUrl); // 根据网址字符串构建URL对象
// 打开URL对象的网络连接,并返回HttpURLConnection连接对象
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET"); // 设置请求方式
setConnHeader(conn, headers);// 设置HTTP连接的头部信息
conn.connect(); // 开始连接
// 打印HTTP调用的应答内容长度、内容类型、压缩方式
Log.d(TAG, String.format("应答内容长度=%d, 内容类型=%s, 压缩方式=%s", conn.getContentLength(), conn.getContentType(), conn.getContentEncoding()) );
// 对输入流中的数据解压和字符编码,得到原始的应答字符串
resp = getUnzipString(conn);
// 打印HTTP调用的应答状态码和应答报文
Log.d(TAG, String.format("应答状态码=%d, 应答报文=%s", conn.getResponseCode(), resp) );
conn.disconnect(); // 断开连接
}
catch (Exception e)
{
e.printStackTrace();
}
return resp;
}
// 从指定url获取图片
public static Bitmap getImage(String callUrl, Map<String, String> headers) {
Bitmap bitmap = null; // 位图对象
try {
Log.d(TAG, "请求地址:"+callUrl);
compatibleSSL(callUrl); // 兼容https开头的调用地址
URL url = new URL(callUrl); // 根据网址字符串构建URL对象
// 打开URL对象的网络连接,并返回HttpURLConnection连接对象
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET"); // 设置请求方式
setConnHeader(conn, headers);// 设置HTTP连接的头部信息
conn.connect(); // 开始连接
// 打印图片获取的应答内容长度、内容类型、压缩方式
Log.d(TAG, String.format("应答内容长度=%d, 内容类型=%s, 压缩方式=%s",
conn.getContentLength(), conn.getContentType(), conn.getContentEncoding()) );
// 对输入流中的数据解码,得到位图对象
bitmap = BitmapFactory.decodeStream(conn.getInputStream());
// 打印图片获取的应答状态码和位图大小
Log.d(TAG, String.format("应答状态码=%d, 位图大小=%s", conn.getResponseCode(), bitmap.getByteCount()) );
conn.disconnect(); // 断开连接
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
// 对指定接口地址发起POST调用
public static String post(String callUrl, String req, Map<String, String> headers) {
String resp = ""; // 应答内容
try {
Log.d(TAG, "请求地址:"+callUrl+", 请求报文="+req);
compatibleSSL(callUrl); // 兼容https开头的调用地址
URL url = new URL(callUrl); // 根据网址字符串构建URL对象
// 打开URL对象的网络连接,并返回HttpURLConnection连接对象
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST"); // 设置请求方式
setConnHeader(conn, headers);// 设置HTTP连接的头部信息
conn.setRequestProperty("Content-Type", "application/json"); // 请求报文为json格式
conn.setDoOutput(true); // 准备让连接执行输出操作。默认为false,POST方式需要设置为true
//conn.setDoInput(true); // 准备让连接执行输入操作。默认为true
conn.connect(); // 开始连接
OutputStream os = conn.getOutputStream(); // 从连接对象中获取输出流
os.write(req.getBytes()); // 往输出流写入请求报文
// 打印HTTP调用的应答内容长度、内容类型、压缩方式
Log.d(TAG, String.format("应答内容长度=%s, 内容类型=%s, 压缩方式=%s",
conn.getHeaderField("Content-Length"), conn.getHeaderField("Content-Type"),
conn.getHeaderField("Content-Encoding")) );
// 对输入流中的数据解压和字符编码,得到原始的应答字符串
resp = getUnzipString(conn);
// 打印HTTP调用的应答状态码和应答报文
Log.d(TAG, String.format("应答状态码=%d, 应答报文=%s", conn.getResponseCode(), resp) );
conn.disconnect(); // 断开连接
} catch (Exception e) {
e.printStackTrace();
}
return resp;
}
// 把文件上传给指定的URL
public static String upload(String uploadUrl, String uploadFile, Map<String, String> headers)
{
String resp = ""; // 应答内容
// 从本地文件路径获取文件名
String fileName = uploadFile.substring(uploadFile.lastIndexOf("/"));
String end = "\r\n"; // 结束字符串
String hyphens = "--"; // 连接字符串
String boundary = "WUm4580jbtwfJhNp7zi1djFEO3wNNm"; // 边界字符串
try (FileInputStream fis = new FileInputStream(uploadFile)) {
Log.d(TAG, "上传地址:"+uploadUrl+", 上传文件="+uploadFile);
compatibleSSL(uploadUrl); // 兼容https开头的调用地址
URL url = new URL(uploadUrl); // 根据网址字符串构建URL对象
// 打开URL对象的网络连接,并返回HttpURLConnection连接对象
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST"); // 设置请求方式
setConnHeader(conn, headers);// 设置HTTP连接的头部信息
conn.setDoOutput(true); // 准备让连接执行输出操作。默认为false,POST方式需要设置为true
//conn.setDoInput(true); // 准备让连接执行输入操作。默认为true
conn.setRequestProperty("Connection", "Keep-Alive"); // 连接过程要保持活跃
// 请求报文要求分段传输,并且各段之间以边界字符串隔开
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
// 根据连接对象的输出流构建数据输出流
DataOutputStream ds = new DataOutputStream(conn.getOutputStream());
// 以下写入请求报文的头部
ds.writeBytes(hyphens + boundary + end);
ds.writeBytes("Content-Disposition: form-data; "
+ "name=\"file\";filename=\"" + fileName + "\"" + end);
ds.writeBytes(end);
// 以下写入请求报文的主体
byte[] buffer = new byte[1024];
int length;
// 先将文件数据写入到缓冲区,再将缓冲数据写入输出流
while ((length = fis.read(buffer)) != -1) {
ds.write(buffer, 0, length);
}
ds.writeBytes(end);
// 以下写入请求报文的尾部
ds.writeBytes(hyphens + boundary + hyphens + end);
ds.close(); // 关闭数据输出流
// 打印HTTP调用的应答内容长度、内容类型、压缩方式
Log.d(TAG, String.format("应答内容长度=%s, 内容类型=%s, 压缩方式=%s",
conn.getHeaderField("Content-Length"), conn.getHeaderField("Content-Type"),
conn.getHeaderField("Content-Encoding")) );
// 对输入流中的数据解压和字符编码,得到原始的应答字符串
resp = getUnzipString(conn);
// 打印HTTP上传的应答状态码和应答报文
Log.d(TAG, String.format("应答状态码=%d, 应答报文=%s", conn.getResponseCode(), resp) );
conn.disconnect(); // 断开连接
}
catch (Exception e)
{
e.printStackTrace();
}
return resp;
}
// 设置HTTP连接的头部信息
private static void setConnHeader(HttpURLConnection conn, Map<String, String> headers)
{
conn.setConnectTimeout(CONNECT_TIMEOUT); // 设置连接的超时时间,单位毫秒
conn.setReadTimeout(READ_TIMEOUT); // 设置读取应答数据的超时时间,单位毫秒
conn.setRequestProperty("Accept", "*/*"); // 设置数据格式
conn.setRequestProperty("Accept-Language", "zh-CN"); // 设置文本语言
conn.setRequestProperty("Accept-Encoding", "gzip, deflate"); // 设置编码格式
// 设置用户代理,包括操作系统版本、浏览器版本等等
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0");
if (headers != null)
{
for (Map.Entry<String, String> item : headers.entrySet())
{
conn.setRequestProperty(item.getKey(), item.getValue());
}
}
}
// 把输入流中的数据按照指定字符编码转换为字符串。处理大量数据需要使用本方法
private static String isToString(InputStream is, String charset)
{
String result = "";
// 创建一个字节数组的输出流对象
try (ByteArrayOutputStream baos = new ByteArrayOutputStream())
{
int i = -1;
while ((i = is.read()) != -1) // 循环读取输入流中的字节数据
{
baos.write(i); // 把字节数据写入字节数组输出流
}
byte[] data = baos.toByteArray(); // 把字节数组输出流转换为字节数组
result = new String(data, charset); // 将字节数组按照指定的字符编码生成字符串
}
catch (Exception e)
{
e.printStackTrace();
}
return result; // 返回转换后的字符串
}
// 从HTTP连接中获取已解压且重新编码后的应答报文
private static String getUnzipString(HttpURLConnection conn) throws IOException
{
String contentType = conn.getContentType(); // 获取应答报文的内容类型(包括字符编码)
String charset = "UTF-8"; // 默认的字符编码为UTF-8
if (contentType != null)
{
if (contentType.toLowerCase().contains("charset=gbk")) // 应答报文采用gbk编码
{
charset = "GBK"; // 字符编码改为GBK
}
else if (contentType.toLowerCase().contains("charset=gb2312")) // 采用gb2312编码
{
charset = "GB2312"; // 字符编码改为GB2312
}
}
String contentEncoding = conn.getContentEncoding(); // 获取应答报文的压缩方式
InputStream is = conn.getInputStream(); // 获取HTTP连接的输入流对象
String result = "";
if (contentEncoding != null && contentEncoding.contains("gzip")) // 应答报文使用gzip压缩
{
// 根据输入流对象构建压缩输入流
try (GZIPInputStream gis = new GZIPInputStream(is))
{
// 把压缩输入流中的数据按照指定字符编码转换为字符串
result = isToString(gis, charset);
}
catch (Exception e)
{
e.printStackTrace();
}
}
else
{
// 把输入流中的数据按照指定字符编码转换为字符串
result = isToString(is, charset);
}
return result; // 返回处理后的应答报文
}
}