gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 11:03:56 24 4
gpt4 key购买 nike

我需要使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com