gpt4 book ai didi

android - 在 Android 中获取 "CURLE_COULDNT_RESOLVE_HOST"错误

转载 作者:行者123 更新时间:2023-11-29 01:55:47 25 4
gpt4 key购买 nike

我已经为 android 编译了静态 libcurl,但不断收到 CurlRes 代码 6,即 CURLE_COULDNT_RESOLVE_HOST。

安卓.mk

LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := Curl

LOCAL_SRC_FILES := prebuild/libcurl.a

include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)

LOCAL_MODULE := ccSharedLib

LOCAL_SRC_FILES := main-jni.cpp

LOCAL_STATIC_LIBRARIES := Curl

include $(BUILD_SHARED_LIBRARY)

主jni.cpp

extern "C" {
size_t write_data(void *ptr, size_t size, size_t count, FILE *stream)
{
size_t written;
written = fwrite(ptr, size, count, stream);
printf("data sent, size = %lu",written);
return written;
}

jint
Java_com_example_testlibcurl_MainActivity_test1( JNIEnv* env,
jobject thiz, jstring downloadDirectoryPath)
{
CURLcode res;
res = curl_global_init(CURL_GLOBAL_ALL);

jint temp = 3;

printf("Method called");
const char *nativeDownloadDirPath = env->GetStringUTFChars(downloadDirectoryPath,0);
// Test code for calling methods of libCURL
CURL *curl;
FILE *fp;
std::string s = "http://travel.paintedstork.com/blog/wp-content/uploads/2012/10/2013-calendar-images-1.jpg";
curl = curl_easy_init();
if(curl)
{
fp = fopen(nativeDownloadDirPath, "wb");
res = curl_easy_setopt(curl, CURLOPT_URL, s.c_str());
res = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
res = curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if(fp)
fclose(fp);
}
return res;
}
}

此代码从网络源下载图像,但每次调用“curl_easy_perform”方法时都会给出错误代码 6。我已经用不同的 URL 检查了这个,但仍然不成功:( ...

list 文件中已提供“android.permission.INTERNET”和“android.permission.WRITE_EXTERNAL_STORAGE”权限。

任何解决这个问题的指针都会有很大的帮助。

最佳答案

确保将有效文件名可写目录路径传递给fopen。

我得到了

Fatal signal 11 (SIGSEGV) at 0x00000010 (code=1) ...

因为 fopen 试图打开目录而不是文件。

从 Eclipse 或使用 adb logcat 检查 LogCat 中的日志以查找其他可能的错误。

我已经通过以下修改测试了您的代码,它可以正常工作。

MainActivity.java:

public class MainActivity
{
private static final String TAG = MainActivity.class.getName();

static
{
try
{
System.loadLibrary("mynativelib");
}
catch (UnsatisfiedLinkError ex)
{
Log.e(TAG, "WARNING: Could not load native library: " + ex.getMessage());
}
}

public static native int DownloadFile(String downloadDirectoryPath);

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

int res = DownloadFile(Environment.getExternalStorageDirectory().getAbsolutePath()
+ File.separator + Environment.DIRECTORY_DOWNLOADS + File.separator
+ "test.jpg");
Log.d(TAG, "Result Code: " + res);
}
}

ma​​in-jni.cpp

#include <jni.h>
#include <android/log.h>
#include <string>
#include <curl/curl.h>

#define LOG_TAG "native"

#define LOG_INFO(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
#define LOG_ERROR(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
#define LOG_WARN(...) __android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)
#define LOG_DEBUG(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)

#ifdef __cplusplus
extern "C"
{
#endif

// [FIX for Android 4.2.x]
// "WARNING: Could not load native library:
// Cannot load library: soinfo_relocate(linker.cpp:975): cannot locate symbol "__exidx_end" referenced by"
// http://stackoverflow.com/a/14501998/313113
void __exidx_start()
{
}
void __exidx_end()
{
}

size_t write_data(void *ptr, size_t size, size_t count, FILE *stream)
{
size_t written;
written = fwrite(ptr, size, count, stream);
LOG_DEBUG("Writing data to file stream %u", written);
return written;
}

jint Java_com_company_awesomeapp_MainActivity_DownloadFile(JNIEnv* env, jobject thiz,
jstring downloadDirectoryPath)
{
CURLcode res;
res = curl_global_init(CURL_GLOBAL_ALL);

jint temp = 3;

LOG_DEBUG("Downloading file");
const char *nativeDownloadDirPath = env->GetStringUTFChars(downloadDirectoryPath, 0);

LOG_DEBUG(nativeDownloadDirPath);

CURL *curl;
FILE *fp;
std::string url = "http://travel.paintedstork.com/blog/wp-content/uploads/2012/10/2013-calendar-images-1.jpg";

curl = curl_easy_init();
if (curl)
{
fp = fopen(nativeDownloadDirPath, "wb");
res = curl_easy_setopt(curl, CURLOPT_URL, url.c_str());

LOG_DEBUG("Before write function");
res = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
LOG_DEBUG("After write function");

LOG_DEBUG("Before write data");
res = curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
LOG_DEBUG("After write data");

res = curl_easy_perform(curl);
curl_easy_cleanup(curl);

if (fp) fclose(fp);
}

env->ReleaseStringUTFChars(downloadDirectoryPath, nativeDownloadDirPath);

return res;
}

/**
* The VM calls JNI_OnLoad when the native library is loaded (for example, through System.loadLibrary).
* JNI_OnLoad must return the JNI version needed by the native library.
*
* @see http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/invocation.html#JNI_OnLoad
*/JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
JNIEnv* env;
if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK)
{
return -1;
}

// [*] Get jclass with env->FindClass.
// [*] Register methods with env->RegisterNatives.
//jniRegisterNativeMethods(env, "dev/android/sample/AndroidNDKSampleActivity", sMethods, NELEM(sMethods));

return JNI_VERSION_1_6;
}

/**
* The VM calls JNI_OnUnload when the class loader containing the native library is garbage collected.
* This function can be used to perform cleanup operations.
*
* Because this function is called in an unknown context (such as from a finalizer),
* the programmer should be conservative on using Java VM services, and refrain from arbitrary
* Java call-backs.
* @see http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/invocation.html#JNI_OnUnload
*/JNIEXPORT void JNI_OnUnload(JavaVM *vm, void *reserved)
{

}


#ifdef __cplusplus
}
#endif

文件将保存在:/storage/emulated/0/Download/test.jpg

关于android - 在 Android 中获取 "CURLE_COULDNT_RESOLVE_HOST"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15263919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com