现在的位置: 首页 > 综合 > 正文

libcurl在android下的移植、编译与测试

2013年10月08日 ⁄ 综合 ⁄ 共 8278字 ⁄ 字号 评论关闭

libcurl主要功能就是用不同的协议连接和沟通不同的服务器~也就是相当封装了的sockPHP 支持libcurl(允许你用不同的协议连接和沟通不同的服务器)。, libcurl当前支持http, https, ftp, gopher, telnet, dict, file, 和ldap 协议。libcurl同样支持HTTPS证书授权,HTTP POST,
HTTP PUT, FTP 上传(当然你也可以使用PHP的ftp扩展), HTTP基本表单上传,代理,cookies,和用户认证。

Libcurl为一个免费开源的,客户端url传输库,支持FTP,FTPS,TFTP,HTTP,HTTPS,GOPHER,TELNET,DICT,FILE和LDAP,跨平台,支持Windows,Unix,Linux等,线程安全,支持Ipv6。并且易于使用。

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

从http://curl.haxx.se/libcurl/ 下载一个稳定的版本,注意选择OS。
在使用之前请大家多阅读libcurl的文档:因为如果要实际运用到项目中,最好对libcurl有具体的了解,具体在
http://curl.haxx.se/libcurl/c/
curl_easy_setopt() 
curl_easy_perform() 
curl_easy_getinfo() 
这三个函数的使用上,需要多去钻研,多看Samples,你才能灵活使用libcurl。
感谢这篇文章:
http://blog.163.com/xu_chao2000/blog/static/27770610200801303252802/
给了我许多启发,再次感谢!

给出我的一个简单的代码例子:
说明:
1.关键在curl_easy_setopt函数设置option,可以设置ftp,http,get,post等许多选项,请根据具体使用情况设置。

2.对取回来的数据需要进行判断,比如http下载文件,如果文件不存在,需要进行处理。因为writer是可以将buf填充404 not found等网页内容的,不能将这个内容当成文件内容,所以需要判断http web返回来的code,进行判断。

3.我有个问题,就是想得到服务器上filename的具体名称,verbose调试已经返回了,但是我在getinfo的时候,试过好多选项,但未找到这个存放真实服务器文件名的选项,如果有知道的麻烦告诉我,谢谢了!

#include "curl/curl.h"
#pragma comment(lib, 
"libcurl.lib")

long writer(void 
*data,
int size, int nmemb, string 
&content);
bool  CurlInit(CURL 
*&curl,
const char* url,string 
&content);
bool  GetURLDataBycurl(const char* URL, string 
&content);

void main()
{
    char *url ="http://www.baidu.com/img/baidu.gif";
    string content;
    if ( GetURLDataBycurl(url,content))
    {
        printf("%s\n",content);

    }

    getchar();
}


bool CurlInit(CURL 
*&curl,
const char* url,string 
&content)
{
    CURLcode code;
    string error;
    curl = curl_easy_init();
    if (curl == NULL)
    {
        printf( "Failed to create CURL connection\n");
        return false;
    }

    code = curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error);
    if (code != CURLE_OK)
    {
        printf( "Failed to set error buffer [%d]\n", code );
        return false;
    }

    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
    code = curl_easy_setopt(curl, CURLOPT_URL, url);
    if (code != CURLE_OK)
    {
        printf("Failed to set URL [%s]\n", error);
        return false;
    }

    code = curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
    if (code != CURLE_OK)
    {
        printf( "Failed to set redirect option [%s]\n", error );
        return false;
    }

    code = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
    if (code != CURLE_OK)
    {
        printf( "Failed to set writer [%s]\n", error);
        return false;
    }

    code = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &content);
    if (code != CURLE_OK)
    {
        printf( "Failed to set write data [%s]\n", error );
        return false;
    }

    return true;
}


long writer(void 
*data,
int size, int nmemb, string 
&content)
{
    long sizes = size * nmemb;
    string temp(data,sizes);
    content += temp; 
    return sizes;
}


bool GetURLDataBycurl(const char* URL,  string 
&content)
{
    CURL *curl = NULL;
    CURLcode code;
    string error;

    code = curl_global_init(CURL_GLOBAL_DEFAULT);
    if (code != CURLE_OK)
    {
        printf( "Failed to global init default [%d]\n", code );
        return false;
    }
 
    
    if ( !CurlInit(curl,URL,content) )
    {
        printf( "Failed to global init default [%d]\n" );
        return PM_FALSE;
    }

    code = curl_easy_perform(curl);
    if (code != CURLE_OK)
    {
        printf( "Failed to get '%s' [%s]\n", URL, error);
        return false;
    }

    long retcode = 0;
    code = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE , &retcode); 
    if ( (code == CURLE_OK) && retcode == 200 )
    {
        double length = 0;
        code = curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD , &length); 
        printf("%d",retcode);
        FILE * file = fopen("1.gif","wb");
        fseek(file,0,SEEK_SET);
        fwrite(content.c_str(),1,length,file);
        fclose(file);

        //struct curl_slist *list;
        
//code = curl_easy_getinfo(curl,CURLINFO_COOKIELIST,&list);
        
//curl_slist_free_all (list);

        return true;
    }

    else
    {
    //    debug1( "%s \n ",getStatusCode(retcode));
        return false;
    }

    curl_easy_cleanup(curl);
    return false;
}

由于项目需要在NDK中使用网络开发,对于c语言网络开发来说,libcurl库是个很不错的选择,但android系统中并没有自带该库,所以就得自己移植了。

下面是移植步骤:

1.  下载curl源码 
我这里下载的是curl-7.22.0,源码下载地址为:http://curl.haxx.se/download.html


2. 准备android源码编译环境

android源码应已全部编译过,具体细节这里不详述,我这里使用的是android2.2 froyo源码树。

 

3.  在android中编译curl 
在最新的curl源码里其实已经带有Android.mk这个编译文件了,而且在这文件的开头注释部分比较详细地介绍编译方法。

1)拷贝curl源码至android源码树下的external/curl

 

2)cd 到 external/curl目录下,输入(红色字部分根据自己的环境做相应的更改):

ANDROID_HOME=/home/braincol/workspace/android/froyo && \

NDK_HOME=/home/braincol/workspace/android/froyo/ndk && \

PATH="$ANDROID_HOME/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/bin:$PATH" \

./configure --host=arm-linux CC=arm-eabi-gcc --with-random=/dev/urandom \

CPPFLAGS="-I$NDK_HOME/platforms/android-8/arch-arm/usr/include \

-I $ANDROID_HOME/external/curl/include/  \

-I $ANDROID_HOME/external/curl/3rd/include   \

-I $ANDROID_HOME/external/curl   \

-I $ANDROID_HOME/out/target/product/generic/obj/STATIC_LIBRARIES/libcurl_intermediates   \

-I $ANDROID_HOME/dalvik/libnativehelper/include/nativehelper   \

-I $ANDROID_HOME/system/core/include   \

-I $ANDROID_HOME/hardware/libhardware/include   \

-I $ANDROID_HOME/hardware/libhardware_legacy/include   \

-I $ANDROID_HOME/hardware/ril/include   \

-I $ANDROID_HOME/dalvik/libnativehelper/include   \

-I $ANDROID_HOME/frameworks/base/include   \

-I $ANDROID_HOME/frameworks/base/opengl/include   \

-I $ANDROID_HOME/frameworks/base/native/include   \

-I $ANDROID_HOME/external/skia/include   \

-I $ANDROID_HOME/out/target/product/generic/obj/include   \

-I $ANDROID_HOME/bionic/libc/arch-arm/include   \

-I $ANDROID_HOME/bionic/libc/include   \

-I $ANDROID_HOME/bionic/libstdc++/include   \

-I $ANDROID_HOME/bionic/libc/kernel/common   \

-I $ANDROID_HOME/bionic/libc/kernel/arch-arm   \

-I $ANDROID_HOME/bionic/libm/include   \

-I $ANDROID_HOME/bionic/libm/include/arch/arm   \

-I $ANDROID_HOME/bionic/libthread_db/include \

-include $ANDROID_HOME/system/core/include/arch/linux-arm/AndroidConfig.h \

-I $ANDROID_HOME/system/core/include/arch/linux-arm/ \

-D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ -D__ARM_ARCH_5E__ -D__ARM_ARCH_5TE__ -DANDROID -DNDEBUG -DNDEBUG -DHAVE_CONFIG_H" \

CFLAGS="-fno-exceptions -Wno-multichar -msoft-float -fpic -ffunction-sections \

-funwind-tables -fstack-protector -Wa,--noexecstack -Werror=format-security \

-fno-short-enums -march=armv5te -mtune=xscale  -Wno-psabi -mthumb-interwork  \

-fmessage-length=0 -W -Wall -Wno-unused -Winit-self -Wpointer-arith \

-Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point  \

-g -Wstrict-aliasing=2 -finline-functions -fno-inline-functions-called-once \

-fgcse-after-reload -frerun-cse-after-loop -frename-registers  -UDEBUG \

-mthumb -Os -fomit-frame-pointer -fno-strict-aliasing -finline-limit=64   \

-Wpointer-arith -Wwrite-strings -Wunused -Winline -Wnested-externs \

-Wmissing-declarations -Wmissing-prototypes -Wno-long-long -Wfloat-equal \

-Wno-multichar -Wsign-compare -Wno-format-nonliteral -Wendif-labels \

-Wstrict-prototypes -Wdeclaration-after-statement -Wno-system-headers"  \

LIBS="-nostdlib -Bdynamic -Wl,-T,$ANDROID_HOME/build/core/armelf.x \

-Wl,-dynamic-linker,/system/bin/linker -Wl,--gc-sections -Wl,-z,nocopyreloc \

-L$ANDROID_HOME/out/target/product/generic/obj/lib -Wl,-z,noexecstack \

-Wl,-rpath-link=$ANDROID_HOME/out/target/product/generic/obj/lib \

-lc -llog -lcutils -lstdc++ \

-Wl,--no-undefined $ANDROID_HOME/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/lib/gcc/arm-eabi/4.4.0/libgcc.a  \

$ANDROID_HOME/out/target/product/generic/obj/lib/crtend_android.o \

-lm $ANDROID_HOME/out/target/product/generic/obj/lib/crtbegin_dynamic.o \

-L$ANDROID_HOME/external/curl/3rd/libs"

 

如果$ANDROID_HOME目录下没有ndk的开发包,那么到google的官网上下载一个放进去就行了。

 

3)cd 到源码根目录 mmm extern/libcurl:

编译完成之后,会生成静态库:out/target/product/generic/obj/STATIC_LIBRARIES/libcurl_intermediates/libcurl.a  。

 

4)如果要生成动态库需要修改curl下的Android.mk : 
LOCAL_PRELINK_MODULE := false 
LOCAL_MODULE:= libcurl 
LOCAL_MODULE_TAGS := optional 
# Copy the licence to a place where Android will find it. 
# Actually, this doesn't quite work because the build system searches 
# for NOTICE files before it gets to this point, so it will only be seen 
# on subsequent builds. 
ALL_PREBUILT += $(LOCAL_PATH)/NOTICE 
$(LOCAL_PATH)/NOTICE: $(LOCAL_PATH)/COPYING | $(ACP) 
$(copy-file-to-target) 
#include $(BUILD_STATIC_LIBRARY) 
include $(BUILD_SHARED_LIBRARY)


4.  在android中测试curl 
1)在android froyo源码树中下建立一个mytest目录,该目录下再建立一个curltest目录。

 

2)在目录curtest下创建curl-test.cpp:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include
"curl/curl.h"
#include
<stdio.h>;
int main()
{
    CURL
*curl;

抱歉!评论已关闭.