org.jssec.android.https.imagesearch.HttpsImageSearch.java Source code

Java tutorial

Introduction

Here is the source code for org.jssec.android.https.imagesearch.HttpsImageSearch.java

Source

/*
 * Copyright (C) 2012-2014 Japan Smartphone Security Association
 *
 * 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.
 */

package org.jssec.android.https.imagesearch;

import javax.net.ssl.SSLException;

import org.apache.http.HttpException;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import android.net.Uri;
import android.os.AsyncTask;

public abstract class HttpsImageSearch extends AsyncTask<String, Void, Object> {

    @Override
    protected Object doInBackground(String... params) {

        // HttpClient2?GET????????finally?shutdown?
        DefaultHttpClient client = new DefaultHttpClient();

        try {
            // --------------------------------------------------------
            // 1??
            // --------------------------------------------------------

            // ?1 URI?https://??
            // ?2 ???????
            String search_url = Uri.parse("https://ajax.googleapis.com/ajax/services/search/images?v=1.0")
                    .buildUpon().appendQueryParameter("q", params[0]).build().toString();
            HttpGet request = new HttpGet(search_url);
            HttpResponse response = client.execute(request);
            checkResponse(response);

            // ?3 HTTPS??????????????
            // ????3.2 ?????
            String result_json = EntityUtils.toString(response.getEntity(), "UTF-8");
            String image_url = new JSONObject(result_json).getJSONObject("responseData").getJSONArray("results")
                    .getJSONObject(0).getString("url");

            // --------------------------------------------------------
            // 2???
            // --------------------------------------------------------

            // ?1 URI?https://??
            // ?2 ???????
            request = new HttpGet(image_url);
            response = client.execute(request);
            checkResponse(response);

            // ?3 HTTPS??????????????
            // ????3.2 ?????
            return EntityUtils.toByteArray(response.getEntity());
        } catch (SSLException e) {
            // ?4 SSLException?????????
            // ??????
            return e;
        } catch (Exception e) {
            return e;
        } finally {
            // ?HttpClientshutdown?
            client.getConnectionManager().shutdown();
        }
    }

    private void checkResponse(HttpResponse response) throws HttpException {
        int statusCode = response.getStatusLine().getStatusCode();
        if (HttpStatus.SC_OK != statusCode) {
            throw new HttpException("HttpStatus: " + statusCode);
        }
    }
}