close

串接 android 其實有個很討厭的事情

就是http的get, post與 https的get, post

官方提供的工具就是要你寫四種方式出來

這實在是太無腦了.

所幸在找尋SDK的時候,意外找到loopj這個套件

用它來建立restfull, 其實超級簡單也很好用 其程式碼如下


public class MyRestFullClient {

    public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler){
        AsyncHttpClient client = new AsyncHttpClient(true, 80, 443);
        client.get(url, params, responseHandler);
    }

    public static void get(Context context, String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
        AsyncHttpClient client = new AsyncHttpClient(true, 80, 443);
        client.get(getAbsoluteUrl(context, url), params, responseHandler);
    }

    public static void post(Context context, String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
        AsyncHttpClient client = new AsyncHttpClient(true, 80, 443);
        client.post(getAbsoluteUrl(context, url), params, responseHandler);
    }

    public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
        AsyncHttpClient client = new AsyncHttpClient(true, 80, 443);
        client.post(url, params, responseHandler);
    }

    private static String getAbsoluteUrl(Context context, String relativeUrl) {
        return context.getString(R.string.api_domain) + relativeUrl;
    }
}

簡單的建立

若要執行ssl 的api串接也很容易

宣告AsyncHttpClient時,使用帶入三個params方法,第一個參數為是否加入SSL 認證, 第二三個參數為 http port與 https port

詳細請看loopj部分程式碼

private static SchemeRegistry getDefaultSchemeRegistry(boolean fixNoHttpResponseException, int httpPort, int httpsPort) {
    if (fixNoHttpResponseException) {
        log.d(LOG_TAG, "Beware! Using the fix is insecure, as it doesn't verify SSL certificates.");
    }

    if (httpPort < 1) {
        httpPort = 80;
        log.d(LOG_TAG, "Invalid HTTP port number specified, defaulting to 80");
    }

    if (httpsPort < 1) {
        httpsPort = 443;
        log.d(LOG_TAG, "Invalid HTTPS port number specified, defaulting to 443");
    }

    // Fix to SSL flaw in API < ICS
    // See https://code.google.com/p/android/issues/detail?id=13117
    SSLSocketFactory sslSocketFactory;
    if (fixNoHttpResponseException) {
        sslSocketFactory = MySSLSocketFactory.getFixedSocketFactory();
    } else {
        sslSocketFactory = SSLSocketFactory.getSocketFactory();
    }

    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), httpPort));
    schemeRegistry.register(new Scheme("https", sslSocketFactory, httpsPort));

    return schemeRegistry;
}

 

當使用該restfull時,其實很簡單只要呼叫該api網址就好了範例如下:

private void initTestSSL(){
    L.d(SplashActivity.class.getSimpleName(), "initTestSSL" );
    String url = "https://api.twitter.com/1/statuses/public_timeline.json";

    MyRestClient.get(url, null, new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
            String msg = new String(responseBody);
            Log.d(TestActivity.class.getSimpleName(), "initTestSSL onSuccess msg:" + msg);
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
            String msg = new String(responseBody);
            Log.d(TestActivity.class.getSimpleName(), "initTestSSL onFailure msg:" + msg);
        }
    });
}

其秀出的log如下:

03-26 23:54:56.361 13275-13275/com.owen.apptest D/TestActivity: initTestSSL
03-26 23:54:57.141 13275-13275/com.owen.apptest D/TestActivity: initTestSSL onFailure msg:{"errors":[{"message":"The Twitter REST API v1 is no longer active. Please migrate to API v1.1. https://dev.twitter.com/docs/api/1.1/overview.","code":64}]}

與開啟其連結為一樣的結果

https://api.twitter.com/1/statuses/public_timeline.json

代表可以正常連線

不過使用公司提供的api時,發現還是不能使用,這讓我感到相當好奇,

我會繼續追下去,若有消息我會持續更新的
 

更新 2017.08.03  

詳細可以看 okhttp3

http://jc7003.pixnet.net/blog/post/306816565-okhttpclient-%E6%95%99%E5%AD%B8

arrow
arrow
    文章標籤
    android api loopj ssl
    全站熱搜

    Owen Chen 發表在 痞客邦 留言(0) 人氣()