Why is it necessary
We want to communicate with the server API to write C + +, and then use a library written in all our applications on different platforms. Of course we want to work under android. Libcurl - the API is a library for transmission, that developers can integrate into their programs; cURL acts as a wrapper for a standalone library libcurl. libcurl is used to provide the ability to transfer files (addressable via URL) multiple applications (both open and commercial). ( Wikipedia ) For iOS, you can download a ready example of connecting and using cURL from the developer. And with iOS simple. Under android google me in the vast could not find any source, wherever produced the successful treatment of this cross-platform library. (Maybe I'm bad looking). And generally forced to work under android cURL turned out a little harder than I would like.
What you will need:
- Installed and configured to work with android Eclipse ;
- ndk and the ability to work with him;
- Skompilennaya under android library cURL .
Getting libraries cURL for android
If you go to a website cURL and go to the download, you can find there skompilenny binary (Android 7.31.0 binary SSL) which apparently can be run from the command-line utility for device. But it is useless if we want to work with the library from your application. Okay Googling can find a tutorial how to collect the necessary ndk *. a library, which can already run the application. there about porting cURL under android and Habre . As a result, we obtain the desired *. A library file. I myself was not collected. I downloaded it honestly.
Further
Next the resulting library can safely insert the project and using the full power ndk access it.
Java part part
Create MainActivity with one button and a field for entering URLs, from which we receive information. activity_main.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="@+id/server_url"
android:text="@string/default_url"
android:hint="@string/server_url_hint"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"/>
<Button android:id="@+id/button_curl_call"
android:layout_below="@+id/server_url"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="@string/button_curl"
android:layout_gravity="right"
/>
<TextView
android:id="@+id/text"
android:layout_below="@+id/button_curl_call"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</RelativeLayout>
MainActivity.java
package com.ifree.ndkNative;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
Public class MainActivity extends Activity {
Private static final String INTENT_HTML_DATA = ". html_data" ;
Public static final int HANDLE_CALLBACK = 0 ;
final Private Handler handler = New Handler () {
@ Override
Public void handleMessage (Message msg) {
Switch (msg.what) {
Case HANDLE_CALLBACK:
String html = msg.getData (). GetString (INTENT_HTML_DATA);
SetHtmlText (html);
Break ;
}
}
};
Private void SetHtmlText (HTML String) {
TextView tv = (TextView) findViewById (R.id.text);
tv.setText (html);
}
/ ** Called when the activity is first created. * /
@ Override
Public void onCreate (Bundle savedInstanceState) {
super . onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
final EditText serverUrl = (EditText) findViewById (R.id.server_url);
Button btnCurl = (Button) findViewById (R.id.button_curl_call);
btnCurl.setOnClickListener ( New OnClickListener () {
@ Override
Public void onClick (View v) {
Native curl = New Native ();
curl.addCurlCallbackListener ( New ICurlCallbackListener () {
@ Override
Public void curlCallBack (String callback) {
Bundle Bundle = New Bundle ();
bundle.putString (INTENT_HTML_DATA, callback);
Message message = handler.obtainMessage ();
message.setData (bundle);
handler.sendMessage (message);
}
});
String response = curl.get_text_from_cpp (String.valueOf (serverUrl.getText ()));
}
});
}
}
Write a class Native.java , which will be an appeal to the C + + code.
package com.ifree.ndkNative;
import java.util.HashSet;
Public class Native {
Private HashSet <ICurlCallbackListener> callBackListeners = New <ICurlCallbackListener> HashSet ();
Public void addCurlCallbackListener (ICurlCallbackListener listener) {
callBackListeners.add (listener);
}
Public void removeCurlCallbackListener (ICurlCallbackListener listener) {
callBackListeners.remove (listener);
}
static {
System.loadLibrary ( "ndkNative" );
}
Public Native get_text_from_cpp String (String Data);
Private void callback (String Data) {
for (ICurlCallbackListener listener: callBackListeners) {
listener.curlCallBack (data);
}
}
}
Must not forget to add to AndroidManifest permit Internet
< uses-permission android: name = "android.permission.INTERNET" />
C + + part
Created by the utility javah file com_ifree_ndkNative_Native.h
# include <jni.h>
# Ifndef _Included_com_ifree_ndkNative_Native
# define _Included_com_ifree_ndkNative_Native
# ifdef __ cplusplus
extern "C" {
# endif
JNIEXPORT jstring JNICALL Java_com_ifree_ndkNative_Native_get_1text_1from_1cpp
(JNIEnv *, jobject, jstring);
# Ifdef __ cplusplus
}
# Endif
# endif
We define a function to get data from the server (JNICALL Java_com_ifree_ndkNative_Native_get_1text_1from_1cpp) inndkNative.cpp
JNIEnv * gEnv;
jobject gObj;
void function_callback (jstring str) {
jclass cls = gEnv-> GetObjectClass (gObj);
jmethodID Mid-gEnv => GetMethodID (cls, "callback" , "(Ljava / lang / String ;) V" ); / / method call from java Native.callback (String data)
gEnv-> CallVoidMethod (gObj, mid, str);
}
size_t function_pt (void * ptr , size_t Size, nmemb size_t, void * Stream ) {
function_callback (gEnv-> NewStringUTF ((char *) ptr));
Written = size_t fwrite (ptr, Size, nmemb, (FILE *) Stream);
if (Written <= 0 )
return Written * Size;
}
JNIEXPORT jstring JNICALL Java_com_ifree_ndkNative_Native_get_1text_1from_1cpp
(JNIEnv * env, jobject obj, jstring str)
{
gEnv = env;
gObj = obj;
CURL * curl ;
CURLcode res;
curl = curl_easy_init ();
if (curl) {
curl_easy_setopt (curl, CURLOPT_URL, env-> GetStringUTFChars (str, 0 ));
curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, function_pt);
res = curl_easy_perform (curl);
curl_easy_cleanup (curl);
/ * Check for errors * /
if (res! = CURLE_OK)
function_callback (gEnv-> NewStringUTF (curl_easy_strerror (res)));
} else {
function_callback (gEnv-> NewStringUTF ( "error" ));
}
return env-> NewStringUTF ( "ok" );
}
Android.mf
LOCAL_PATH : = $ ( call my-dir)
include $ ( CLEAR_VARS )
include $ ( CLEAR_VARS )
LOCAL_MODULE : = libcurl
LOCAL_SRC_FILES : = libcurl.a
include $ ( PREBUILT_STATIC_LIBRARY )
include $ ( CLEAR_VARS )
LOCAL_MODULE : = ndkNative
LOCAL_SRC_FILES : = ndkNative.cpp
LOCAL_STATIC_LIBRARIES : = libcurl
include $ ( BUILD_SHARED_LIBRARY )
Source
https://dl.dropboxusercontent.com/u/38942741/ndkNative.zip
No comments:
Post a Comment