我需要使用Volley库获取rss feed但是Volley一直忽略我的format=feed&type=rss
Params。
我尝试了所有的东西,但我无法让它发挥作用
这是我的网址: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.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();
}
答案 0 :(得分:0)
如果您尝试过对网址进行编码并且无法正常工作,则问题出在凌空。不幸的是,我遇到了同样的情况,只有使用截击的POST请求。他们在HTTP实现方面完美无瑕,但在凌空时返回400.我当时的解决方案是坚持使用旧的HTTP实现。
答案 1 :(得分:0)
请使用以下网址获取所需的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&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
使用此最终网址和下面的示例代码,您将获得 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响应,我没有尝试过,但是,您可以阅读以下问题以获取更多信息