4

我无法让我的 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;
}

在我的 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
4

1 回答 1

4

使用本机网络仍然需要更新您的 android 清单以允许从应用程序进行网络操作:

<uses-permission android:name="android.permission.INTERNET" />
于 2013-04-11T14:08:38.887 回答