0

外部の場所(Webサーバー)からデータを取得しているときにUIを更新する簡単で便利で自然な方法を見つけることができないため、この質問はしばらく私を悩ませました。

私は私のシナリオをスケッチします:

JSONArray と JSONObject を提供する Web サーバーを取得しました。私はヌルポインターが好きではないので、常にこれを返します。を拡張するクラスを書こうとしましたAsyncTask。私はこれを行うことに成功し、次のようになります。

public class Server extends AsyncTask<String, Integer, Object>{

        private String fetch_url;
        private String return_type;
        private Activity ct;

        public Server() {
        }

        public Server(Activity ct) {
            this.ct = ct;
        }

        @Override
        protected void onPreExecute()
        {


        }


        @Override
        protected Object doInBackground(String... url) {
            this.fetch_url = url[0];
            this.return_type = url[1];

            if(return_type.equals("json_object")) {
                return urlToJsonObject(this.fetch_url);
            } else {
                return urlToJsonArray(this.fetch_url);
            } 



        }




        private String convertStreamToString(InputStream is) {
           /*
            * To convert the InputStream to String we use the BufferedReader.readLine()
            * method. We iterate until the BufferedReader return null which means
            * there's no more data to read. Each line will appended to a StringBuilder
            * and returned as String.
            */

    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
           StringBuilder sb = new StringBuilder();

           String line = null;
           try {
               while ((line = reader.readLine()) != null) {
                   sb.append(line + "\n");
               }
           } catch (IOException e) {
               e.printStackTrace();
           } finally {
               try {
                   is.close();
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }

           return sb.toString();
        }

    protected JSONObject urlToJsonObject(String url) {
        JSONObject arr = null;

        HttpClient httpclient = new DefaultHttpClient();

        // Prepare a request object
        HttpGet httpget = new HttpGet(url); 

        // Execute the request
        HttpResponse response;
        try {
            response = httpclient.execute(httpget);

            // Get hold of the response entity
            HttpEntity entity = response.getEntity();

            if (entity != null) {

                // A Simple JSON Response Read
                InputStream instream = entity.getContent();
                String result= convertStreamToString(instream);

                // A Simple JSONArray Creation
                arr = new JSONObject(result);

                // Closing the input stream will trigger connection release
                instream.close();


            }


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

        return arr;
    }

    protected JSONArray urlToJsonArray(String url) {
        JSONArray arr = null;

        HttpClient httpclient = new DefaultHttpClient();

        // Prepare a request object
        HttpGet httpget = new HttpGet(url); 

        // Execute the request
        HttpResponse response;
        try {
            response = httpclient.execute(httpget);

            // Get hold of the response entity
            HttpEntity entity = response.getEntity();

            if (entity != null) {

                // A Simple JSON Response Read
                InputStream instream = entity.getContent();
                String result= convertStreamToString(instream);

                // A Simple JSONArray Creation
                arr = new JSONArray(result);

                // Closing the input stream will trigger connection release
                instream.close();


            }


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

        return arr;
    }

    }

データが入ってきたら、ゲッターとセッターを使用して独自のモデルに変換します。次に、これらの結果で UI を更新したいのですが、何らかの理由で常に nullpointers を受け取ります。

例えば ​​:

  • 複数の PhotoAlbum で埋めたい GridLayout を取得しました。
  • まず、Web サーバーからこれらのレコードを取得しています。
  • 次に、各アルバムを処理します。
  • 次に、個々の Album が GridLayout に追加された場合は、更新する必要があります。そして、UIをブロックしている間、一度にすべてではありません。

外部ソースからデータを取得し、読み込み中に UI を更新するための優れた中心的な方法は何ですか? 誰かがテクニック、チュートリアル、方法、洞察などを教えてくれますか?私はこれについて非常に興味があります.

4

1 に答える 1