【问题标题】:User Agent on a url.getContent();url.getContent() 上的用户代理;
【发布时间】:2012-03-27 16:13:42
【问题描述】:

在连接到其他 url 后,我需要从 xml 提供的 url 下载一个文件。我的问题是我需要相同的用户代理和 ip 来执行这两个操作。

为了获取 xml,我使用托管在我的服务器上的 PHP 代码并将其发送给我的应用程序的用户代理:

String ua=new WebView(ct).getSettings().getUserAgentString().trim();
ua = ua.replaceAll(" ", "%20");

然后我用一个通用的 RssParserSax 解析 xml,它给了我我需要的一切。

之后,我尝试将文件下载到可绘制的 var 中,然后这样做:

Drawable dd = ImageOperations(url);

private Drawable ImageOperations(String url) {
        try {
            InputStream is = (InputStream) this.fetch(url);
            Drawable d = Drawable.createFromStream(is, "src");
            return d;
        } catch (MalformedURLException e) {
            return null;
        } catch (IOException e) {
            return null;
        }
    }

private Object fetch(String address) throws MalformedURLException,IOException {
        URL url = new URL(address);
        Object content = url.getContent();
        return content;
    }

但由于我不发送用户代理,它没有给我任何东西。

【问题讨论】:

    标签: android user-agent


    【解决方案1】:

    最后我这样做是为了解决:

    Bitmap bmImg;
    
    try {
        URL url = new URL(addres);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    
        conn.addRequestProperty("User-Agent", ua);
        conn.setDoInput(true);
        conn.connect();
        InputStream is = conn.getInputStream();
    
        bmImg = BitmapFactory.decodeStream(is);
        imagen.setImageBitmap(bmImg);
        imagen.setScaleType(ScaleType.FIT_XY);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用 AndroidHttpClient.newInstance(useragent)。

      在此处查看示例AndroidHttpClient can not getEntity().getContent() after closed

      【讨论】:

        猜你喜欢
        • 2012-12-25
        • 2018-10-02
        • 1970-01-01
        • 2010-10-22
        • 1970-01-01
        • 2022-06-13
        • 1970-01-01
        • 2017-08-26
        • 1970-01-01
        相关资源
        最近更新 更多