2013-04-11 12 views
2

我無法讓我的POCO C++庫在Android上發送請求,它只會拋出一個錯誤,因爲無法找到地址,下面是我的代碼。爲什麼它不工作?我一直在使用this作爲參考,它需要在android和ios上都能運行。Android上的Poco C++ Net找不到地址

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 

回答

2

使用本地網絡仍然需要您的Android清單進行更新,以允許從應用程序的網絡操作:

<uses-permission android:name="android.permission.INTERNET" />