Qt Jambi:访问 QNetworkReply 的内容
我在访问 QNetworkReply 对象的内容时遇到问题。内容似乎为空或为零。根据文档(从 c++ 翻译为 java),我认为我已经正确设置了此设置,但无济于事。此外,还报告了“未知错误”。
任何想法都非常感激。代码:
public class Test extends QObject {
private QWebPage page;
public Test() {
page = new QWebPage();
QNetworkAccessManager nac = new QNetworkAccessManager();
nac.finished.connect(this, "requestFinished(QNetworkReply)");
page.setNetworkAccessManager(nac);
page.loadProgress.connect(this, "loadProgress(int)");
page.loadFinished.connect(this, "loadFinished()");
}
public void requestFinished(QNetworkReply reply) {
reply.reset();
reply.open(OpenModeFlag.ReadOnly);
reply.readyRead.connect(this, "ready()"); // never gets called
System.out.println("bytes: " + reply.url().toString()); // writes out asset uri no problem
System.out.println("bytes: " + reply.bytesToWrite()); // 0
System.out.println("At end: " + reply.atEnd()); // true
System.out.println("Error: " + reply.errorString()); // "Unknown error"
}
public void loadProgress(int progress) {
System.out.println("Loaded " + progress + "%");
}
public void loadFinished() {
System.out.println("Done");
}
public void ready() {
System.out.println("Ready");
}
public void open(String url) {
page.mainFrame().load(new QUrl(url));
}
public static void main(String[] args) {
QApplication.initialize(new String[] { });
Test t = new Test();
t.open("http://news.bbc.co.uk");
QApplication.exec();
}
}
I'm having trouble accessing the content of QNetworkReply objects. Content appears to be empty or zero. According to the docs (translating from c++ to java) I think I've got this set up correctly, but to no avail. Additionally an "Unknown error" is being reported.
Any ideas much appreciated. Code:
public class Test extends QObject {
private QWebPage page;
public Test() {
page = new QWebPage();
QNetworkAccessManager nac = new QNetworkAccessManager();
nac.finished.connect(this, "requestFinished(QNetworkReply)");
page.setNetworkAccessManager(nac);
page.loadProgress.connect(this, "loadProgress(int)");
page.loadFinished.connect(this, "loadFinished()");
}
public void requestFinished(QNetworkReply reply) {
reply.reset();
reply.open(OpenModeFlag.ReadOnly);
reply.readyRead.connect(this, "ready()"); // never gets called
System.out.println("bytes: " + reply.url().toString()); // writes out asset uri no problem
System.out.println("bytes: " + reply.bytesToWrite()); // 0
System.out.println("At end: " + reply.atEnd()); // true
System.out.println("Error: " + reply.errorString()); // "Unknown error"
}
public void loadProgress(int progress) {
System.out.println("Loaded " + progress + "%");
}
public void loadFinished() {
System.out.println("Done");
}
public void ready() {
System.out.println("Ready");
}
public void open(String url) {
page.mainFrame().load(new QUrl(url));
}
public static void main(String[] args) {
QApplication.initialize(new String[] { });
Test t = new Test();
t.open("http://news.bbc.co.uk");
QApplication.exec();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想 QWebPage 已经查询了所有进来的 QNetworkReply 对象。文档说
所以重置它是行不通的。
请注意,如果您直接通过
QNetworkAccessManager
发出网络请求,您确实会得到一个包含可用数据的QNetworkReply
。不过,您只能获得一个实际 URL,而QWebPage
方法会下载此后的所有其他内容,即图像、js 等。I imagine that the
QWebPage
has already queried all theQNetworkReply
objects as they came in. The docs sayso resetting it isn't going to work.
Note that if you make the network request directly through
QNetworkAccessManager
you do get aQNetworkReply
with data available. You only get one though for the actual URL whereas theQWebPage
approach downloads everything else after that i.e. images, js, etc.