草庐IT

android - Volley 库忽略 format=feed&type=rss 参数

coder 2023-12-24 原文

我需要使用 Volley 库获取 rss 提要,但 Volley 一直忽略我的 format=feed&type=rss 参数。
我尝试了所有方法,但无法正常工作。
这是我的网址:http://almesryoon.com/%D8%AF%D9%81%D8%AA%D8%B1-%D8%A3%D8%AD%D9%88%D8%A7% D9%84-%D8%A7%D9%84%D9%88%D8%B7%D9%86?format=feed&type=rss
注意:此网址适用于 Google PostMan
我的 SimpleXmlRequest 类:

public class SimpleXmlRequest<T> extends Request<T> {

    private static final Serializer serializer = new Persister();
    private final Class<T> clazz;
    private final Map<String, String> headers;
    private final Listener<T> listener;


    /**
     * Make HTTP request and return a parsed object from Response
     * Invokes the other constructor.
     *
     * @see SimpleXmlRequest#SimpleXmlRequest(int, String, Class, Map, Listener, ErrorListener)
     */
    public SimpleXmlRequest(int method, String url, Class<T> clazz,
                            Listener<T> listener, ErrorListener errorListener) {
        this(method, url, clazz, null, listener, errorListener);
    }


    /**
     * Make HTTP request and return a parsed object from XML Response
     *
     * @param url URL of the request to make
     * @param clazz Relevant class object
     * @param headers Map of request headers
     * @param listener
     * @param errorListener
     *
     */
    public SimpleXmlRequest(int method, String url, Class<T> clazz, Map<String, String> headers,
                            Listener<T> listener, ErrorListener errorListener) {
        super(method, url, errorListener);
        this.clazz = clazz;
        this.headers = headers;
        this.listener = listener;
    }


    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        return headers != null ? headers : super.getHeaders();
    }


    @Override
    protected void deliverResponse(T response) {
        listener.onResponse(response);
    }

    @Override
    public String getBodyContentType() {
        return "application/xml";
    }

    @Override
    protected Response<T> parseNetworkResponse(NetworkResponse response)
    {
        try {

            String data = new String(response.data, HttpHeaderParser.parseCharset(response.headers));// The problem here this data is not XML

            return Response.success(serializer.read(clazz, data),
                    HttpHeaderParser.parseCacheHeaders(response));
        }
        catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        }
        catch (Exception e) {
            return Response.error(new VolleyError(e.getMessage()));
        }
    }
}

我还尝试使用 METHOD.POSTgetParams() 方法
目标 rss:

<?xml version="1.0" encoding="utf-8"?>
<!-- generator="Joomla! - Open Source Content Management" -->
<rss version="2.0" 
    xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <title>Title</title>
        <description>Description</description>
        <link>http://google.com</link>
        <lastBuildDate>Thu, 31 Mar 2016 01:27:12 +0200</lastBuildDate>
        <generator>Joomla! - Open Source Content Management</generator>
        <atom:link rel="self" type="application/rss+xml" href="http://example.com/example?format=feed&amp;type=rss"/>
        <language>ar-aa</language>
        <item>
            <title>Item1</title>
            <link>http://example.com/example1</link>
            <guid isPermaLink="true">http://example.com/example1</guid>
            <description>Description1</description>
            <author>admin</author>
            <category>Category1</category>
            <pubDate>Wed, 30 Mar 2016 18:49:37 +0200</pubDate>
        </item>
        <item>
            <title>Item2</title>
            <link>http://example.com/example2</link>
            <guid isPermaLink="true">http://example.com/example2</guid>
            <description>Description2</description>
            <author>admin</author>
            <category>Category1</category>
            <pubDate>Wed, 30 Mar 2016 18:49:37 +0200</pubDate>
        </item>
    </channel>
</rss>

我正在使用这段代码,它运行良好,但是 HttpParamsBasicHttpParamsHttpConnectionParamsDefaultHttpClientHttpGetHttpResponseHttpEntity 现在已弃用。
我的工作代码:

StringBuilder chaine = new StringBuilder("");

        try {

            HttpParams httpParameters = new BasicHttpParams();
            // Set the timeout in milliseconds until a connection is established.
            // The default value is zero, that means the timeout is not used.
            int timeoutConnection = AppConstants.CONNECTION_TIMEOUT_SEC * 1000;
            HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
            // Set the default socket timeout (SO_TIMEOUT)
            // in milliseconds which is the timeout for waiting for data.
            int timeoutSocket = AppConstants.SOCKET_TIMEOUT_SEC * 1000;
            HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

            // request method is GET
            DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
            HttpGet httpGet = new HttpGet(url);

            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();

            InputStream inputStream = httpEntity.getContent();

            BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream));

            String line;

            while ((line = rd.readLine()) != null) {

                chaine.append(line);

            }

            rd.close();

        } catch (IOException e) {
            // writing exception to log
            e.printStackTrace();
        }

最佳答案

请使用以下网址获取您需要的 RSS

https://m.almesryoon.com/%d8%af%d9%81%d8%aa%d8%b1-%d8%a3%d8%ad%d9%88%d8%a7%d9%84-%d8%a7%d9%84%d9%88%d8%b7%d9%86?format=feed&type=rss&template=mobile

这是日志:

...
D/dalvikvm: GC_FOR_ALLOC freed 27K, 12% free 6432K/7303K, paused 20ms, total 20ms
I/onResponse: <?xml version="1.0" encoding="utf-8"?>
              <!-- generator="Joomla! - Open Source Content Management" -->
              <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
                <channel>
                    <title>دفتر أحوال الوطن - المصريون</title>
                    <description>المصريون صحيفة يومية مستقلة تنشر آخر وأهم أخبار مصر والوطن العربى والعالم</description>
                    <link>https://m.almesryoon.com/دفتر-أحوال-الوطن</link>
                    <lastBuildDate>Thu, 23 Feb 2017 04:36:51 +0200</lastBuildDate>
                    <generator>Joomla! - Open Source Content Management</generator>
                    <atom:link rel="self" type="application/rss+xml" href="https://m.almesryoon.com/دفتر-أحوال-الوطن?format=feed&amp;type=rss&amp;template=mobile"/>
                    <language>ar-aa</language>
                    <item>
                    ...

在用你的服务器 url 测试了一些示例代码后,我发现它得到了 301 响应。从您的第一个网址,第二个网址将是 (https)

https://almesryoon.com/%D8%AF%D9%81%D8%AA%D8%B1-%D8%A3%D8%AD%D9%88%D8%A7%D9%84-%D8%A7%D9%84%D9%88%D8%B7%D9%86?format=feed&type=rss

第三个网址将是:

http://m.almesryoon.com/%d8%af%d9%81%d8%aa%d8%b1-%d8%a3%d8%ad%d9%88%d8%a7%d9%84-%d8%a7%d9%84%d9%88%d8%b7%d9%86?template=mobile

最终的 url 将是 (https):

https://m.almesryoon.com/%d8%af%d9%81%d8%aa%d8%b1-%d8%a3%d8%ad%d9%88%d8%a7%d9%84-%d8%a7%d9%84%d9%88%d8%b7%d9%86?template=mobile

使用此最终 URL 和下面的示例代码,您将获得 200 响应代码。我认为您可以使用我上面提到的最终 URL 来尝试您的代码。

RequestQueue queue = Volley.newRequestQueue(this);
String url = "https://m.almesryoon.com/%d8%af%d9%81%d8%aa%d8%b1-%d8%a3%d8%ad%d9%88%d8%a7%d9%84-%d8%a7%d9%84%d9%88%d8%b7%d9%86?template=mobile";
StringRequest request = new StringRequest(url, new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        Log.i("onResponse", response);
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        Log.e("onErrorResponse", error.toString());
    }
});

queue.add(request); 

关于在Volley中处理301响应,我没有尝试过,但是,您可以阅读以下问题以获取更多信息

Android volley to handle redirect

关于android - Volley 库忽略 format=feed&type=rss 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36319630/

有关android - Volley 库忽略 format=feed&type=rss 参数的更多相关文章

  1. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  2. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  3. ruby-on-rails - 'compass watch' 是如何工作的/它是如何与 rails 一起使用的 - 2

    我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t

  4. ruby-on-rails - 如何从 format.xml 中删除 <hash></hash> - 2

    我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为

  5. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

  6. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  7. ruby-on-rails - 如何在 ruby​​ 中使用两个参数异步运行 exe? - 2

    exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby​​中使用两个参数异步运行exe吗?我已经尝试过ruby​​命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何ruby​​gems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除

  8. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  9. ruby - 在 jRuby 中使用 'fork' 生成进程的替代方案? - 2

    在MRIRuby中我可以这样做:deftransferinternal_server=self.init_serverpid=forkdointernal_server.runend#Maketheserverprocessrunindependently.Process.detach(pid)internal_client=self.init_client#Dootherstuffwithconnectingtointernal_server...internal_client.post('somedata')ensure#KillserverProcess.kill('KILL',

  10. ruby - RSpec - 使用测试替身作为 block 参数 - 2

    我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere

随机推荐