使用libcurl第三方庫實現Android非同步任務,libcurlandroid
本文承接自前篇部落格將Cocos2d-x的libcurl單獨打包到Android
在此基礎上,又進行了進一步的使用:
1)增加libcurl非同步方法呼叫
2)實現Android非同步任務
下面直接上代碼:
1】jni之first.c:first.h無變化,first.c添加如下代碼,切記C變數必須把聲明方法函數的開始
/* * Copyright (C) 2009 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */#include "first.h"int first(int x, int y){ return x + y;}#define MULTI_CURL_NUM 3char* URL = "http://www.baidu.com";char* PROXY = "";unsigned int TIMEOUT = 10000;char* urlA[3] = {"http://www.baidu.com","http://www.cocoachina.com/bbs/read.php?tid=228963","http://www.cnblogs.com/zhaoyl/p/4001151.html"};//非同步方法呼叫擷取返回資料size_t curl_writer(void* buffer,size_t size,size_t count,void* stream){// std::string* pStream = static_cast<std::string*>(stream);// (*pStream).append((char*)buffer,size * count);// log("pStream=%s",pStream); return size*count;}//增加URL請求,非同步實質上是多個同步請求的組合CURL* curl_easy_handler(const char* sUrl,const char* sProxy,char* sRsp,unsigned int uiTimeout){ CURL* curl = curl_easy_init(); curl_easy_setopt(curl, CURLOPT_URL,sUrl); curl_easy_setopt(curl, CURLOPT_NOSIGNAL,1); if(uiTimeout > 0) { curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS,uiTimeout); } if(sProxy[0]!='\0') { curl_easy_setopt(curl, CURLOPT_PROXY,sProxy); } curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,curl_writer); curl_easy_setopt(curl, CURLOPT_WRITEDATA,&sRsp);// log("curl_write = %lu",sizeof(sRsp)); return curl;}//select方法,選擇當前可讀寫的檔案int curl_multi_select(CURLM* curl_m){ int ret = 0; int idx1 = 0; struct timeval timeout_tv; fd_set fd_read; fd_set fd_write; fd_set fd_except; int max_fd = -1; int ret_code; FD_ZERO(&fd_read); FD_ZERO(&fd_write); FD_ZERO(&fd_except); timeout_tv.tv_sec = 60; timeout_tv.tv_usec = 0; curl_multi_fdset(curl_m, &fd_read, &fd_write, &fd_except, &max_fd);// log("max_fd = %d",max_fd); if(max_fd == -1) { return -1; }// ret_code = ::select(max_fd+1, &fd_read, &fd_write, &fd_except, &timeout_tv); ret_code = select(max_fd+1, &fd_read, &fd_write, &fd_except, &timeout_tv);// log("ret_code = %d",ret_code); switch (ret_code) { case -1: ret = -1; break; case 0: default: ret = 0; break; } return ret;}//非同步方法呼叫入口int curl_multi_demo(int num,CURLcode* result){int idx;static int count = 0; CURLM* curl_m = curl_multi_init(); char* RspArray[num]; int running_handles = 0; int msgs_left; CURLMsg* msg;// int s[num]; CURL* CurlArray[num]; for(idx = 0; idx < num; ++idx) { CurlArray[idx] = NULL; CurlArray[idx] = curl_easy_handler(URL, PROXY, RspArray[idx], TIMEOUT); if(CurlArray[idx] == NULL) { return -1; } curl_multi_add_handle(curl_m, CurlArray[idx]); }// log("running_handles out up = %d",running_handles); while(CURLM_CALL_MULTI_PERFORM == curl_multi_perform(curl_m, &running_handles)) {// log("running_handles = %d",running_handles); }// log("running_handles out = %d",running_handles); while(running_handles) { if(-1 == curl_multi_select(curl_m)) {// log( "select error" ); break; }else{ while(CURLM_CALL_MULTI_PERFORM == curl_multi_perform(curl_m, &running_handles)) {// log("select:%d ",running_handles); } }// log("select: %d",running_handles); } while ((msg = curl_multi_info_read(curl_m, &msgs_left))) { if(CURLMSG_DONE == msg->msg); { int idx; for(idx = 0; idx < num; ++idx) { if(msg->easy_handle == CurlArray[idx]) break; } if(idx == num) {// log("curl not found"); }else{// log("curl [%d] completed with status: %d",idx,msg->data.result);// log("rsp: %s",RspArray[idx]); result[count++] = msg->data.result;//將求求結果狀態傳入result數組,該數組是指標 } } } for(idx = 0; idx < num; ++idx) { curl_multi_remove_handle(curl_m, CurlArray[idx]); } for(idx = 0; idx < num; ++idx) { curl_easy_cleanup(CurlArray[idx]); } curl_multi_cleanup(curl_m); return 0;}//同步方法入口int curl_easy_demo(int num){int idx = 0; char* RspArray[num]; for(idx = 0; idx < num; ++idx) { CURL* curl = curl_easy_handler(URL, PROXY, RspArray[idx], TIMEOUT); CURLcode code = curl_easy_perform(curl);// log("curl [%d] completed with status:%d",idx,code); curl_easy_cleanup(curl); } return 0;}
2】jni之second.c
#include "first.h"#include <jni.h>#include <curl/curl.h>#include <stdio.h>#include <stdlib.h>#include <sys/time.h>#include <unistd.h>//extern "C"jintJava_com_example_libdemo_MainActivity_add( JNIEnv* env, jobject this, jint x, jint y){/*//libcurl同步方法案例CURL* curl;CURLcode res;curl = curl_easy_init();if(NULL == curl){return first(1,999);} curl_easy_setopt(curl, CURLOPT_URL,"http://www.baidu.com"); curl_easy_setopt(curl, CURLOPT_READFUNCTION,NULL); curl_easy_setopt(curl, CURLOPT_NOSIGNAL,1); curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT,20); curl_easy_setopt(curl, CURLOPT_TIMEOUT,20); res = curl_easy_perform(curl); curl_easy_cleanup(curl);*///libcurl非同步方法呼叫案例CURLcode result[3];int sum; gettimeofday(&begin_tv, NULL); curl_multi_demo(3,result); gettimeofday(&end_tv, NULL); long eclapsed = (end_tv.tv_sec - begin_tv.tv_sec)*1000 + (end_tv.tv_usec - begin_tv.tv_usec) / 1000;// log("eclapsed time: %ld ms",eclapsed); sum = (int)result[0] + (int)result[1]; return first(sum,(int)result[2]);}3】src之MainActivity.java
package com.example.libdemo;import org.apache.http.client.HttpClient;import org.apache.http.impl.client.DefaultHttpClient;import android.os.AsyncTask;import android.os.Bundle;import android.support.v7.app.ActionBarActivity;import android.util.Log;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ProgressBar;import android.widget.TextView;public class MainActivity extends ActionBarActivity {private Button button;private TextView tv;private TextView textView;private ProgressBar progressBar;private static final String TAG = "ASYNC_TASK";private int x = 123; private int y = 456; private MyTask mTask; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); System.loadLibrary("twolib-second"); tv = (TextView)findViewById(R.id.textView2); button = (Button) this.findViewById(R.id.button1); textView = (TextView) findViewById(R.id.textView3); this.button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) { mTask = new MyTask(); mTask.execute("test");}}); } public native int add(int x,int y); @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } //非同步任務 private class MyTask extends AsyncTask<String, Integer, String> { @Override protected void onPreExecute() { // TODO Auto-generated method stub// super.onPreExecute(); Log.i(TAG, "onPreExecute() called"); textView.setText("Loading..."); } //處理耗時任務 @Override protected String doInBackground(String... arg0) { // TODO Auto-generated method stub try { int z = add(x, y); tv = (TextView) findViewById(R.id.textView2); tv.setText( "The sum dskdshd of " + x + " and " + y + " is " + z ); return new String("test"); } catch (Exception e) { Log.e(TAG, e.getMessage()); } return null; } @Override protected void onProgressUpdate(Integer... values) { // TODO Auto-generated method stub// super.onProgressUpdate(values); progressBar.setProgress(values[0]); textView.setText("loading..." + "%"); } @Override protected void onPostExecute(String result) { // TODO Auto-generated method stub// super.onPostExecute(result); textView.setTag(result); } } }
注意:
1、關於代碼中用到的UI,讀者自行拖到layout中,此處不在詳細介紹
2、Android中目前比較常用的為handle機制,由於作者Android水平有限,選擇了最簡單的非同步任務實現。有興趣的讀者,自行實現其他方法
3、運行:點擊最左側按鈕後,返回下面的結果,如果是0表面程式中的3個URL請求成功,如果返回其他則失敗,curl返回錯誤碼見上面部落格。