Question

i have a problem in my app. At start i will describe how it works. It send request to the server (which wasn't created by me), and in the response i am getting JSON string ( it's not so big average 10 records in JSONarray) there in one parameter there is URL to the picture which i download and save as a bitmap for every JSON object. To summarize i download a JSON which looks like:

{
    "id":"125", 
    "description":"desc",   
    "type":"type",  
    "state":"state",    
    "date":"2012-09-22 10:40:46.0",
    "lat":"52.321911",
    "lng":"19.464111",
    "author":"user",
    "photo":"GetImage?size=small&id=0",
    "comments":[

    ]
    }

x 10 for example, and then i download from URL "photo" image for every object. Problem lies in the time of execution, it is really really long which it should't it is not big data. Here it is how i do this: AsyncClass that download image:

private class HttpGetImage extends AsyncTask<Object, Integer, Integer>
    {
        public boolean ready = false;
        public boolean success = false;

        @Override
        protected Integer doInBackground(Object... obj) {
            DefaultHttpClient client = new MyHttpClient(ViewEdit.this);

            HttpGet get = new HttpGet(url+photoUrl);
            HttpResponse getResponse = null;
            try {
                getResponse = client.execute(get);
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                ready=true; return null;

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                ready=true; return null;

            }

            HttpEntity responseEntity = getResponse.getEntity();
            BufferedHttpEntity httpEntity = null;
            try {
                httpEntity = new BufferedHttpEntity(responseEntity);
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
                ready=true; return null;

            }
            InputStream imageStream = null;
            try {
                imageStream = httpEntity.getContent();
                m_orders.get((Integer)obj[1])
                    .setOrderBitmap(BitmapFactory.decodeStream(imageStream));

                success = true;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                ready=true; return null;

            } finally {
                try {
                    imageStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    ready=true; return null;

                }
            }

            ready = true;
            return null;
        }
    }

and AsyncClass for jsonString:

private class HttpGetNotifications extends AsyncTask<Double, Integer, Integer>
    {
        public boolean ready = false;
        public boolean success = false;

        @Override
        protected Integer doInBackground(Double... params) 
        {
            DefaultHttpClient client = new MyHttpClient(ViewEdit.this);
            HttpGet get = new HttpGet(url + Double.toString(params[0]) + "&longitude=" + Double.toString(params[1]) + "&radius="+distance);

            HttpResponse getResponse = null;
            try {
                getResponse = client.execute(get);

            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                ready=true; return null;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                ready=true; return null;
            }
            HttpEntity responseEntity = getResponse.getEntity();
            String entityContents="";
            try {
                entityContents = EntityUtils.toString(responseEntity);

                loadNotifications(entityContents);
                success = true;
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                ready=true; return null;

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                ready=true; return null;

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                ready=true; return null;

            }

            ready = true;
            return null;
        }

        public void loadNotifications(String jsonstring) throws JSONException
        {
            JSONObject jsonResponse = new JSONObject(jsonstring);

            JSONArray notifi = jsonResponse.getJSONArray("notifications");
            for (int i =0, count = notifi.length(); i <count; i++){
                //storage of data
            }       
        }
    }

Maybe you guys have an idea how can i optimize that code to reduce a time of execution?

Was it helpful?

Solution

Put in some profiling to find what's taking the time. In each of your AsyncTasks:

private long time0, time1;
@Override protected void onPreExecute() {
    time0 = System.currentTimeMillis();
}
@Override protected void onPostExecute(HttpResponse response) {
    time1 = System.currentTimeMillis();
long deltaT = (time1 - time0);
Log.d(TAG, "Execute took "+deltaT+"ms");
}

And then go from there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top