日常开发的时候,会遇到各种各样的Url.这里就总结一些常见的Url遇到的一些问题,以及对应的处理方式
常见问题
- 参数问题
- 重定向问题
- Url长度问题
- Url传递过程中编码问题
1.Url 参数处理
1.1 获取Url 指定参数的值
fun getQueryParameterValue(url: String, key: String): String? {
if (TextUtils.isEmpty(url) || TextUtils.isEmpty(key)) return url
var uri = Uri.parse(url)
if (uri.isOpaque()) {
return url
}
//利用Map的唯一性拼接参数
var parameterMap = getParameterMap(url)
return parameterMap.get(key)
}
fun getQueryParameterDecodevalue(url: String, key: String): String? {
if (TextUtils.isEmpty(url) || TextUtils.isEmpty(key)) return ""
var uri = Uri.parse(url)
if (uri.isOpaque()) {
return ""
} else {
return Uri.parse(url).getQueryParameter(key)
}
}
private fun getParameterMap(
url: String
): HashMap<String, String> {
var map: HashMap<String, String> = HashMap<String, String>()
// 参数名字的列表
var parameter = Uri.parse(url).queryParameterNames
parameter.forEach {
if (getQueryParameterDecodevalue(url, it) != null)
map.put(it, getQueryParameterDecodevalue(url, it)!!)
}
return map
}
1.2 添加和删除指定参数
fun deleteQueryParameterDecodevalue(url: String, key: String): String {
if (TextUtils.isEmpty(url) || TextUtils.isEmpty(key)) return url
var uri = Uri.parse(url)
if (uri.isOpaque()) {
return url
}
if (!url.contains("?")) {
url + "?"
}
var urlStart = url.split("?").get(0)
//利用Map的唯一性拼接参数
var parameterMap = getParameterMap(url)
//利用map的唯一性 存储或者更新值
if (parameterMap.containsKey(key)){
parameterMap.remove(key)
}
//参数的map
var appendUrl = appendMapParameter(parameterMap)
var division = "?"
return "$urlStart$division$appendUrl"
}
fun addQueryParameterDecodevalue(url: String, key: String, value: String): String {
if (TextUtils.isEmpty(url) || TextUtils.isEmpty(key)) return url
var uri = Uri.parse(url)
if (uri.isOpaque()) {
return url
}
if (!url.contains("?")) {
url + "?"
}
var urlStart = url.split("?").get(0)
//利用Map的唯一性拼接参数
var parameterMap = getParameterMap(url)
//利用map的唯一性 存储或者更新值
parameterMap.put(key, Uri.encode(value))
//参数的map
var appendUrl = appendMapParameter(parameterMap)
var division = "?"
return "$urlStart$division$appendUrl"
}
fun addQueryParameterValue(url: String, key: String, value: String): String {
if (TextUtils.isEmpty(url) || TextUtils.isEmpty(key)) return url
var uri = Uri.parse(url)
if (uri.isOpaque()) {
return url
}
if (url.contains("#")) {
url.replace("#", "%23")
}
if (!url.contains("?")) {
url + "?"
}
var urlStart = url.split("?").get(0)
//利用Map的唯一性拼接参数
var parameterMap = getParameterMap(url)
//利用map的唯一性 存储或者更新值
parameterMap.put(key, value)
//参数的map
var appendUrl = appendMapParameter(parameterMap)
var division = "?"
return "$urlStart$division$appendUrl"
}
private fun appendMapParameter(parameterMap: HashMap<String, String>): String {
var stringBuilder = StringBuilder()
parameterMap.keys.forEach {
stringBuilder.append(it).append("=").append(parameterMap.get(it)).append("&")
}
if (stringBuilder.length > 0) {
stringBuilder.deleteCharAt(stringBuilder.length - 1)
}
return stringBuilder.toString()
}
private fun getParameterMap(
url: String
): HashMap<String, String> {
var map: HashMap<String, String> = HashMap<String, String>()
// 参数名字的列表
var parameter = Uri.parse(url).queryParameterNames
parameter.forEach {
if (getQueryParameterDecodevalue(url, it) != null)
map.put(it, getQueryParameterDecodevalue(url, it)!!)
}
return map
}
2.获取重定向地址的真实地址
package com.wu.base.util;
import android.text.TextUtils;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.concurrent.TimeUnit;
import io.reactivex.Observable;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.ObservableSource;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class UrlRedirectUrlUtil {
//是否Encode
private static boolean isEncode = false;
private static Disposable callDisposable;
//获取重定向后的真实地址
public static String getRedirectUrl(String path) {
boolean isEncode = false;
if (TextUtils.isEmpty(path)) {
return "";
}
if (findEnd(path)) {
return path;
}
if (isShortUrl(path))
return path;
try {
if (path.contains("#")) {
path = path.replace("#", URLEncoder.encode("#"));
isEncode = true;
}
OkHttpClient mOkHttpClient = new OkHttpClient();
Request request = new Request.Builder()
.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,**;q=0.8");
builder.header("Accept-Encoding", "gzip, deflate, br");
builder.header("Accept-Language", "zh-CN,zh;q=0.9");
builder.header("Connection", "keep-alive");
builder.header("User-Agent", "Mozilla/5.0 (Linux; Android 5.0; AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Mobile Safari/537.36");
Request request = builder.url(s).get().build();
OkHttpClient client = new OkHttpClient()
.newBuilder()
.followRedirects(false)
.connectTimeout(10, TimeUnit.SECONDS)//设置连接超时时间
.writeTimeout(10, TimeUnit.SECONDS)
.readTimeout(10, TimeUnit.SECONDS)//设置读取超时时间
.build();
client.writeTimeoutMillis();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
observer.onError(new Throwable("解析失败"));
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String path = url;
if (response.code() == HttpURLConnection.HTTP_MOVED_TEMP || response.code() == HttpURLConnection.HTTP_MOVED_PERM) {
String location = response.headers().get("Location");
if (!TextUtils.isEmpty(location)) {
path = location;
}
if (isEncode) {
path = path.replace("%23", URLDecoder.decode("%23"));
isEncode = false;
}
}
observer.onNext(path);
observer.onComplete();
}
});
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
path -> callBack.getRedirectSuccess(path),
throwable -> callBack.getRedirectFail());
}
// 取消请求
public static void cancelRequest() {
if (callDisposable != null && !callDisposable.isDisposed()) {
callDisposable.dispose();
}
}
public static boolean isShortUrl(String url) {
return false;
}
public static boolean findEnd(String url) {
if (url.endsWith(".mp4")) {
return true;
}
return false;
}
public interface ResponseCallBack {
void getRedirectSuccess(String url);
void getRedirectFail();
}
}
3.处理地址过长问题
日常开发的时候有一些三方SDK对Url的长度做出了限制,再加上日常使用过程中Url贼长让需要对Url做操作的同事极度反感,这个时候就需要对很长的Url做处理,以下是个人对长连接做的处理
- 缩短器:后台来个长变短的服务,其实也就是一个字符串变短的算法,移动端请求接口即可
- 对Url的参数进行编码使参数变短
4.Url参数编码问题
Url在分享,浏览器打开的时候中文或者特殊字符会影响Url的使用,所以在给Url处理参数的时候,给参数做统一编码(Encode)显得特别重要.假如是复杂和长期维护的项目,建议项目架构的时候就处理掉这个问题,不然后期会疯(亲身经历).
总结
日常开发中,会遇到各种各样的Url,所以涉及到的Url处理也就各种各样,建议将Url做一个统一进出的工具 统一管理各种Url的操作.其中涉及到的Url参数转码问题要慎重,要提前和H5和IOS沟通好防止出现Url处理不同意问题.