Load images in a gallery and ElasticSearch
<p>I would like to "link" two programs together, separately they work, in one hand I have a gallery of images downloaded from the web and in other hand I have a program which catch up urls (in a string) of images after querying a ElasticSearch server.</p>

<p>My goal is to display the result (=images) of my query in a gallery. I guess I miss a step but I can't find what step. Do I need to use AsyncTask for this?</p>

<p>Catlog :</p>

<pre><code> W/dalvikvm(9421): Unable to resolve superclass of Lnet/sf/json/JSONException; (740)
 W/dalvikvm(9421): Link of class 'Lnet/sf/json/JSONException;' failed
 W/dalvikvm(9421): VFY: unable to resolve exception class 629 (Lnet/sf/json/JSONException;)
 W/dalvikvm(9421): VFY: unable to find exception handler at addr 0xff
 W/dalvikvm(9421): VFY:  rejected Lcom/example/androidgridweb/AndroidGridLayoutActivity;.ESE (Lcom/example/androidgridweb/ImageAdapter;)V
 W/dalvikvm(9421): VFY:  rejecting opcode 0x0d at 0x00ff
 W/dalvikvm(9421): VFY:  rejected Lcom/example/androidgridweb/AndroidGridLayoutActivity;.ESE (Lcom/example/androidgridweb/ImageAdapter;)V
 W/dalvikvm(9421): Verifier rejected class Lcom/example/androidgridweb/AndroidGridLayoutActivity;
 W/dalvikvm(9421): Class init failed in newInstance call (Lcom/example/androidgridweb/AndroidGridLayoutActivity;)
 D/AndroidRuntime(9421): Shutting down VM
 W/dalvikvm(9421): threadid=1: thread exiting with uncaught exception (group=0x40acb228)
 E/AndroidRuntime(9421): FATAL EXCEPTION: main
 java.lang.VerifyError: com/example/androidgridweb/AndroidGridLayoutActivity
    at java.lang.Class.newInstanceImpl(Native Method)
    at java.lang.Class.newInstance(Class.java:1319)
    at android.app.Instrumentation.newActivity(Instrumentation.java:1039)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2101)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2229)
    at android.app.ActivityThread.access$600(ActivityThread.java:139)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1261)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:4945)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    at dalvik.system.NativeStart.main(Native Method)
</code></pre>

<p>AndroidGridLayoutActivity.java</p>

<pre><code>public class AndroidGridLayoutActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.grid_layout);

    GridView gridView = (GridView) findViewById(R.id.grid_view);

    // Instance of ImageAdapter Class
    ImageAdapter imgAdapter =new ImageAdapter(this);
    imgAdapter.addmThumbIds("http://i724.photobucket.com/albums/ww246/bnhenson/squishy.jpg");
    imgAdapter.addmThumbIds("http://xc0.xanga.com/08fe2b7a30c37281496919/m224298958.jpg");
    imgAdapter.addmThumbIds("http://cdn4.teen.com/wp-content/uploads/2012/07/paranorman-zayn-malik-tweets.jpg");

        try {
            eSearchElastic.ESE(imgAdapter);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
    gridView.setAdapter(imgAdapter);

} 
}
</code></pre>

<p>ImageAdapter.java</p>

<pre><code>public class ImageAdapter extends BaseAdapter {
private Context mContext;
Bitmap bmImg;

public static List&lt;String&gt; mThumbIds = new ArrayList&lt;String&gt;();

public void addmThumbIds(String url){
    mThumbIds.add(url);
}

// Constructor
public ImageAdapter(Context c){
    mContext = c;
}

@Override
public int getCount() {
    return mThumbIds.size();
}

@Override
public Object getItem(int position) {
    return mThumbIds.get(position);
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {
    imageView = new ImageView(mContext);
    downloadFile(imageView, mThumbIds.get(position));
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imageView.setLayoutParams(new GridView.LayoutParams(135, 135));
    imageView.setPadding(0, 0, 1, 0);
    }
    else{
        imageView=(ImageView) convertView;
        downloadFile(imageView, mThumbIds.get(position));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(new GridView.LayoutParams(135, 135));
        imageView.setPadding(0, 0, 1, 0);
    }
    return imageView;
}
void downloadFile(final ImageView imageView, String fileUrl) {

    AsyncTask&lt;Object, Object, String&gt; task = new AsyncTask&lt;Object, Object, String&gt;() {

        @Override
        protected String doInBackground(Object... params) {
            URL myFileUrl = null;
            try {
                myFileUrl = new URL((String) params[0]);
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                HttpURLConnection conn = (HttpURLConnection) myFileUrl
                        .openConnection();
                conn.setDoInput(true);
                conn.connect();
                InputStream is = conn.getInputStream();

                bmImg = BitmapFactory.decodeStream(is);

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return null;
        }
        protected void onPostExecute(String unused) {
            imageView.setImageBitmap(bmImg);
        }
    };
    task.execute(fileUrl);

}
}
</code></pre>

<p>ESearchElastic.java</p>

<pre><code>public class eSearchElastic{



public static void ESE (final ImageAdapter imgAdapter)throws ClientProtocolException, IOException {

        String server = "servername";
        String index = "images";
        String type = "images_schema_1";
        StringEntity query = null;
        try {
            query = new StringEntity("{\"sort\" : [ {\"confidence_level\" : {\"order\" : \"desc\"} }],\"from\" : 0, \"size\" : 10,\"query\" : {\"text_phrase\" : { \"keyword\" : \"finding nemo\"}},\"filter\" : {\"numeric_range\" : {\"confidence_level\" : { \"from\" : 10, \"to\" : 100, \"include_lower\" : true, \"include_upper\" : true}}}}'");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        int i=0;
        HttpClient httpclient = new DefaultHttpClient();
        StringBuilder urlStr = new StringBuilder("http://");
         urlStr.append(server)
                .append("/" + index)
                .append("/" + type + "/_search");
         System.out.println(urlStr.toString());
         while (i &lt; 5) {
            HttpPost httpost = new HttpPost(urlStr.toString());
            httpost.setEntity((HttpEntity)query);

         httpost.setHeader("Accept", "application/json");
            httpost.setHeader("Content-type", "application/json");

             HttpResponse response = null;
            try {
                response = httpclient.execute(httpost);
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

             String answer = null;
            try {
                answer = org.apache.http.util.EntityUtils.toString(response.getEntity());
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println(answer);
            i++;

            JSONObject answerJson = (JSONObject) JSONSerializer.toJSON(answer);
            String urlImage;
            try {
                urlImage = answerJson.getString("hits");
                JSONObject answerJson1 = (JSONObject) JSONSerializer.toJSON(urlImage);
                String urlImage1 = answerJson1.getString("hits").replace("[", "").replace("]","");
                JSONObject answerJson2 = (JSONObject) JSONSerializer.toJSON(urlImage1);
                String urlImage2 = answerJson2.getString("_source");
                JSONObject answerJson3 = (JSONObject) JSONSerializer.toJSON(urlImage2);
                String urlImage3 = answerJson3.getString("url");
                System.out.println(urlImage3);
                imgAdapter.addmThumbIds(urlImage3);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

         }
    }
}
</code></pre>

<hr>

<p>Addition/modification :</p>

<p>eSearchElastic.java :</p>

<pre><code> public static void ESE (final ImageAdapter imgAdapter)throws ClientProtocolException, IOException {

AsyncTask&lt;Void, Void, Void&gt; task = new AsyncTask&lt;Void, Void, Void&gt;(){

    @Override
    protected Void doInBackground(Void... params) {
        String server = "esearch.gumgum.com";
        String index = "images";
        String type = "images_schema_1";
        StringEntity query = null;
        try {
            query = new StringEntity("{\"sort\" : [ {\"confidence_level\" : {\"order\" : \"desc\"} }],\"from\" : 0, \"size\" : 10,\"query\" : {\"text_phrase\" : { \"keyword\" : \"finding nemo\"}},\"filter\" : {\"numeric_range\" : {\"confidence_level\" : { \"from\" : 10, \"to\" : 100, \"include_lower\" : true, \"include_upper\" : true}}}}'");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        int i=0;
        HttpClient httpclient = new DefaultHttpClient();
        StringBuilder urlStr = new StringBuilder("http://");
         urlStr.append(server)
                .append("/" + index)
                .append("/" + type + "/_search");
         System.out.println(urlStr.toString());
         while (i &lt; 5) {
            HttpPost httpost = new HttpPost(urlStr.toString());
            httpost.setEntity((HttpEntity)query);

         httpost.setHeader("Accept", "application/json");
            httpost.setHeader("Content-type", "application/json");

             HttpResponse response = null;
            try {
                response = httpclient.execute(httpost);
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

             String answer = null;
            try {
                answer = org.apache.http.util.EntityUtils.toString(response.getEntity());
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println(answer);
            i++;

            JSONObject answerJson = (JSONObject) JSONSerializer.toJSON(answer);
            String urlImage;
            try {
                urlImage = answerJson.getString("hits");
                JSONObject answerJson1 = (JSONObject) JSONSerializer.toJSON(urlImage);
                String urlImage1 = answerJson1.getString("hits").replace("[", "").replace("]","");
                JSONObject answerJson2 = (JSONObject) JSONSerializer.toJSON(urlImage1);
                String urlImage2 = answerJson2.getString("_source");
                JSONObject answerJson3 = (JSONObject) JSONSerializer.toJSON(urlImage2);
                String urlImage3 = answerJson3.getString("url");
                System.out.println(urlImage3);
                imgAdapter.addmThumbIds(urlImage3);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

         }
        return null;
    }

};
task.execute();

}
</code></pre>

<p>New Catlog:</p>

<pre><code>E/AndroidRuntime(10619): FATAL EXCEPTION: main
E/AndroidRuntime(10619): java.lang.VerifyError: com/example/androidgridweb/eSearchElastic$1
    at com.example.androidgridweb.eSearchElastic.ESE(eSearchElastic.java:28)
    at com.example.androidgridweb.AndroidGridLayoutActivity.onCreate(AndroidGridLayoutActivity.java:44)
    at android.app.Activity.performCreate(Activity.java:4531)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2150)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2229)
    at android.app.ActivityThread.access$600(ActivityThread.java:139)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1261)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:4945)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    at dalvik.system.NativeStart.main(Native Method)
</code></pre>
