c++ - 如何使用 Android ndk 进行 HTTP 请求

标签 c++ http android-ndk

我正在为 android/ios 编写一个纯 native 应用程序,但在网络代码方面遇到了问题。我似乎无法让 curl 进行编译,无论哪种方式,我都希望有一个更面向对象的库。

用什么方法做这个的好库是什么?我目前从 HTTP 请求中获取 json 的方法是使用 SDL2_Net 的代码

class HTTPRequest{
protected:
    std::map<std::string, std::string> uris;
    std::string url;

public:
    std::string host;

    HTTPRequest(std::string path) : url(path), host("182.50.154.140") {}

    void addURI(std::string parameter, std::string value){
        uris[parameter] = value;
    }

    std::string sendRequest(bool useCookie=false){
        std::string temppath = url;
        if(!uris.empty()){
            temppath += '?';
            for(auto &a : uris){
                temppath += a.first;
                temppath += '=';
                temppath += a.second;
                temppath += '&';
            }
        }
        IPaddress* addr = new IPaddress;
        SDLNet_ResolveHost(addr, host.c_str(), 8080);
        TCPsocket sock = SDLNet_TCP_Open(addr);
        std::string a = "GET ", b = " HTTP/1.1\r\nHost: 182.50.154.140:8080\r\nConnection: close\r\n\r\n";
        std::string c = a+temppath+b;
        SDLNet_TCP_Send(sock, c.c_str(), c.length());
        std::string d;
        char* buf = new char[1024];
        while(SDLNet_TCP_Recv(sock, buf, 1024) > 0){
            d += buf;
            delete[] buf;
            buf = new char[1024];
        }
        delete[] buf;
        SDLNet_TCP_Close(sock);
        int p = d.find("{");
        d.erase(0, p-1);
        return d;
    }
};

不过这会在大页面上中断。

最佳答案

cURL 是您的最佳选择。这里有几个链接,您可以尝试移植 cURL

http://thesoftwarerogue.blogspot.com/2010/05/porting-of-libcurl-to-android-os-using.html https://github.com/jahrome/curl-android

如果您仍然无法使用 cURL 成功,请尝试 POCO

http://pocoproject.org/

关于c++ - 如何使用 Android ndk 进行 HTTP 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25677711/

相关文章:

c++ - 如何将 IDirect3DSurface9 或 LPD3DXBUFFER 以 JPG 格式保存到内存

http - HTTPS 主机 : header? 中显式默认端口的定义行为

android - 设置 CMAKE_PREFIX_PATH 不适用于 Android 工具链

Android Gradle 添加静态库

c# - 阻止 HttpWebRequest 检查我的负载

Java JNI - 将 C 中分配的资源与 java 对象相关联?

c++ - 如何对模板类中的不同模板参数执行不同的操作?

c++ - 继承类的标签调度

C++ opengl着色器编译失败

javascript - 使用文件类型输入发布 $http 请求并获取 req.body 和 req.file 不使用 multer nodejs