NDK HTTP: Using CURL

http://curl.haxx.se/libcurl/

#include <curl/curl.h>

main.c: error: 'shutdown' redeclared as different kind of symbol

Renamed:
static bool shutdown;
to:
static bool shutdownFlag;

Project builds ok.

http://curl.haxx.se/libcurl/c/

http://curl.haxx.se/libcurl/c/example.html

http://curl.haxx.se/libcurl/c/simple.html

void getPage(const char* url)
{
  CURL *curl;
  CURLcode res;

  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, url);
    res = curl_easy_perform(curl);

    /* always cleanup */
    curl_easy_cleanup(curl);
  }
}

undefined reference to `curl_easy_init'

In IDE: Project menu -> Properties -> C/C++ General -> Paths and Symbols -> Libraries tab -> Add... button -> "curl" -> OK -> OK

Project builds ok.

Ran project.
res = 0 = CURLE_OK

How to actually get the data?

http://curl.haxx.se/libcurl/c/libcurl-tutorial.html

size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);

char* http_data;
size_t http_data_num_bytes;

// Data was recieved. This might be the entire response, or it might be part of the
// response, in which case this function will be called multiple times.
size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp)
{
    // The number of additional bytes received.
    size_t num_bytes = size * nmemb;

    // The previous number of bytes which had been received.
    size_t prev_http_data_num_bytes = http_data_num_bytes;

    // Memory allocation.
    if (http_data_num_bytes > 0)
    {
        // Increase the memory used to store the result to make room
        // for this next part.
        http_data_num_bytes += num_bytes;
        http_data = (char*)realloc(http_data, http_data_num_bytes);
    }
    else
    {
        // Initial allocation for first part of response received.
        http_data_num_bytes = num_bytes;
        http_data = (char*)malloc(num_bytes);
    }

    // Copy the new data.
    if (num_bytes > 0)
    {
        memcpy(&(http_data[prev_http_data_num_bytes]), buffer, num_bytes);
    }

    return num_bytes;
}

// HTTP download
void getPage(const char* url)
{
    CURL *curl;
    CURLcode res;

    curl = curl_easy_init();
    if (curl)
    {
        http_data_num_bytes = 0;

        curl_easy_setopt(curl, CURLOPT_URL, url);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
        //curl_easy_setopt(curl, CURLOPT_WRITEDATA, &http_data);

        res = curl_easy_perform(curl);

        fprintf(stderr,"res: %i\n", res);
        fprintf(stderr,"data: %s\n", http_data);

        /* always cleanup */
        curl_easy_cleanup(curl);
    }
}

Note that the code above uses hard coded http_data and http_data_num_bytes variables, where if you uncomment the call to:

curl_easy_setopt(curl, CURLOPT_WRITEDATA, &http_data);

... then the write_data function's userp argument can be used, which is a better general purpose solution.