0

アプリに問題があります。最初に、それがどのように機能するかを説明します。サーバー(私が作成したものではありません)にリクエストを送信し、応答でJSON文字列(JSONarrayの平均10レコードはそれほど大きくありません)を取得しています。1つのパラメーターに、ダウンロードした画像へのURLがあります。すべてのJSONオブジェクトのビットマップとして保存します。要約すると、次のようなJSONをダウンロードします。

{
    "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":[

    ]
    }

たとえばx10の場合、すべてのオブジェクトのURL「写真」画像からダウンロードします。問題は実行時間にあります。それは本当に長いので、ビッグデータではないはずです。これが私がこれを行う方法です:画像をダウンロードするAsyncClass:

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;
        }
    }

およびjsonStringのAsyncClass:

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
            }       
        }
    }

たぶん皆さんは、実行時間を短縮するためにそのコードを最適化する方法を知っていますか?

4

1 に答える 1

0

時間を取っているものを見つけるためにいくつかのプロファイリングを入れてください。各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");
}

そしてそこから行きます。

于 2012-11-14T22:21:39.220 に答える