Dav*_*son 4 android android-ndk poco-libraries
我无法让我的 POCO C++ 库在 Android 上发送请求,它只会抛出一个错误,因为找不到地址,下面是我的代码。为什么不起作用?我一直用这个作为参考,希望它能在 android 和 ios 上运行。
int NetPoco::getJson(string url){
try
{
printnet("Prepare session");
// prepare session
URI uri("www.google.se");
HTTPClientSession session(uri.getHost(), uri.getPort());
// prepare path
printnet("Prepare path");
string path(uri.getPathAndQuery());
if (path.empty()) path = "/";
// send request
printnet("Prepare request for");
printnet(path.c_str());
HTTPRequest req(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1);
session.sendRequest(req);
// get response
printnet("Get response");
HTTPResponse res;
cout << res.getStatus() << " " << res.getReason() << endl;
// print response
printnet("Print response");
istream &is = session.receiveResponse(res);
StreamCopier::copyStream(is, cout);
printnet("RESULT");
}
catch (Exception &ex)
{
printnet(ex.displayText().c_str());
//cerr << ex.displayText() << endl;
return -1;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当在我的 Android 设备上运行时,我只得到:
04-11 15:33:30.492: D/NET_TAG(16118): Prepare session
04-11 15:33:30.492: D/NET_TAG(16118): Prepare path
04-11 15:33:30.492: D/NET_TAG(16118): Prepare request for
04-11 15:33:30.492: D/NET_TAG(16118): www.google.se
04-11 15:33:30.500: D/NET_TAG(16118): No address found
Run Code Online (Sandbox Code Playgroud)
使用本机网络仍然需要更新您的 Android 清单以允许从应用程序进行网络操作:
<uses-permission android:name="android.permission.INTERNET" />
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
405 次 |
| 最近记录: |