加入收藏 | 设为首页 | 会员中心 | 我要投稿 马鞍山站长网 (https://www.0555zz.cn/)- 媒体处理、内容创作、云渲染、网络安全、业务安全!
当前位置: 首页 > 综合聚焦 > 编程要点 > 语言 > 正文

Android Url的一些常见问题

发布时间:2023-10-19 10:43:47 所属栏目:语言 来源:
导读:日常开发的时候,会遇到各种各样的Url.这里就总结一些常见的Url遇到的一些问题,以及对应的处理方式
常见问题
参数问题
重定向问题
Url长度问题
Url传递过程中编码问题
1.Url 参数处理
1.1 获取Url 指定参数的值
日常开发的时候,会遇到各种各样的Url.这里就总结一些常见的Url遇到的一些问题,以及对应的处理方式
常见问题
参数问题
重定向问题
Url长度问题
Url传递过程中编码问题
1.Url 参数处理
1.1 获取Url 指定参数的值

 /**
     * 获取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)

    }

    /**
     *  获取Url参数的值(Decode之后的)
     *
     *  getQueryParameter()  方法是默认Decode的
     *
     */
    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)
        }
    }

  /**
     * 把url 的参数转为Map存储
     */
    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;

/**
 * @author wkq
 * @date 2022年07月22日 16:15
 * @des 重定向Url处理工具
 */

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")
                    .addHeader("Accept-Encoding", "gzip, deflate, br")
                    .addHeader("Accept-Language", "zh-CN,zh;q=0.9")
                    .addHeader("Connection", "keep-alive")
                    .addHeader("User-Agent", "Mozilla/5.0 (Linux; Android 5.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Mobile Safari/537.36")
                    .url(path)
                    .build();
            Call mCall = mOkHttpClient.newCall(request);
            Response response = mCall.execute();
            String backUrl = response.header("Location");
            if (response.code() != HttpURLConnection.HTTP_MOVED_TEMP && response.code() != HttpURLConnection.HTTP_MOVED_PERM) {
                String requestPath = response.request().url().toString();
                if (isEncode) {
                    requestPath = requestPath.replace("%23", URLDecoder.decode("%23"));
                    isEncode = false;
                }
                return requestPath;
            } else {
                return getRedirectUrl(backUrl);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return path;
        }
    }


    //处理重定向的Url
    public static void getRedirectUrl(String url, ResponseCallBack callBack) {
        if (callBack == null) return;
        //取消上一个请求
        cancelRequest();
        if (TextUtils.isEmpty(url)) {
            callBack.getRedirectFail();
            return;
        }
        if (findEnd(url)) {
            callBack.getRedirectSuccess(url);
            return;
        }
        if (isShortUrl(url)) {
            callBack.getRedirectSuccess(url);
            return;
        }


        callDisposable = Observable
                .create((ObservableOnSubscribe<String>) emitter -> {
                    if (url.contains("#")) {
                        isEncode = true;
                        emitter.onNext(url.replace("#", URLEncoder.encode("#")));
                    } else {
                        emitter.onNext(url);
                    }
                    emitter.onComplete();
                })
                .flatMap((Function<String, ObservableSource<String>>) s -> new Observable<String>() {
                    @Override
                    protected void subscribeActual(Observer<? super String> observer) {
                        Request.Builder builder = new Request.Builder();
                        builder.header("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();
        }
    }

    /**
     * 判断是否是是讯云短连接
     *
     * @param url
     * @return
     */
    public static boolean isShortUrl(String url) {

        return false;

    }

    /**
     * 判断外部链接.MP4结尾
     */
    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的参数进行编码使参数变短
 

(编辑:马鞍山站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章