我尝试了所有的东西,但我无法让它工作.
这是我的网址:http://almesryoon.com/دفتر-أحوال-الوطن?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,Map<String,String> headers,ErrorListener errorListener) { super(method,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.POST与getParams()方法
目标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&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>
我正在使用此代码并且它工作正常,但现在不推荐使用httpParams,BasichttpParams,httpconnectionParams,DefaulthttpClIEnt,httpGet,httpResponse和httpentity.
我的工作代码:
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 20msI/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&type=RSS&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
最后的网址是(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个响应代码.我想你可以用我上面提到的最终网址尝试你的代码.
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 Params全部内容,希望文章能够帮你解决android – Volley库忽略format = feed&type = rss Params所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)