zoukankan      html  css  js  c++  java
  • [Android] 开源框架 xUtils HttpUtils 代理设置 (Temporary Redirect错误)

    今天简单学习了一下xUtils的使用

    https://github.com/wyouflf/xUtils

    其中用到HttpUtils模块时,发现总是出现Temporary Redirect 错误。

    查看源代码,发现如果是通过代理上网的话,需要在初始化HttpUtils时,传代理的String值。

    在HttpUtils.java中:

        public HttpUtils(int connTimeout, String userAgent) {
            HttpParams params = new BasicHttpParams();
    
            ConnManagerParams.setTimeout(params, connTimeout);
            HttpConnectionParams.setSoTimeout(params, connTimeout);
            HttpConnectionParams.setConnectionTimeout(params, connTimeout);
    
            if (TextUtils.isEmpty(userAgent)) {
                userAgent = OtherUtils.getUserAgent(null);
            }
            HttpProtocolParams.setUserAgent(params, userAgent);
    ...

    所以,解决方法一,调用需要传代理的构造方法来实例化HttpUtils.

        public HttpUtils(String userAgent) {
            this(HttpUtils.DEFAULT_CONN_TIMEOUT, userAgent);
        }

    不过,很多情况下,我们是不需要知道这些的,因为不同的环境可能需要不同的代理设置。

    解决方法二,直接注释掉设置代理的代码

        public HttpUtils(int connTimeout, String userAgent) {
            HttpParams params = new BasicHttpParams();
    
            ConnManagerParams.setTimeout(params, connTimeout);
            HttpConnectionParams.setSoTimeout(params, connTimeout);
            HttpConnectionParams.setConnectionTimeout(params, connTimeout);
    
            if (TextUtils.isEmpty(userAgent)) {
                userAgent = OtherUtils.getUserAgent(null);
            }
            //HttpProtocolParams.setUserAgent(params, userAgent);

    这样,你在代码这个层面就不需要考虑代理的设置了。

  • 相关阅读:
    456. 132 Pattern
    496. Next Greater Element I
    503. Next Greater Element II
    341. Flatten Nested List Iterator
    232. Implement Queue using Stacks
    225. Implement Stack using Queues
    208. Implement Trie (Prefix Tree)
    思考--为何早晨型人更容易成功
    Listary的使用
    【运维】虚拟机如何安装CentOS
  • 原文地址:https://www.cnblogs.com/lemonbar/p/3968200.html
Copyright © 2011-2022 走看看