1

post / getにjの非同期ライブラリを使用しています(自分で試してみましたが、同じことが起こりました)。問題が発生し続けます。getNews()メソッドを呼び出すと、ProgressBar(不確定)がフリーズし続けます。私はここで何が間違っているのですか?

protected static ArrayList<HashMap<String, String>> allNews = new ArrayList<HashMap<String, String>>();

public void getNews() throws JSONException
{

    VestiHTTPClient.get("json.php", null, new JsonHttpResponseHandler()
    {

        @Override
        public void onSuccess(JSONArray news)
        {
            try
            {
                for(int i=0; i<news.length(); i++)
                {
                    JSONObject jo = (JSONObject) news.get(i);
                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put("POST_URL", jo.getString("POST_URL"));
                    map.put("TOPIC_TITLE", jo.getString("TOPIC_TITLE"));
                    map.put("AUTHOR", jo.getString("AUTHOR"));
                    map.put("CATEGORY", jo.getString("CATEGORY"));
                    map.put("POST_TIME", jo.getString("POST_TIME"));
                    map.put("POST_TEXT", jo.getString("POST_TEXT"));
                    map.put("IMAGE", jo.getString("IMAGE"));

                    allNews.add(map);                       
                }
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }

        @Override
         public void onFinish() {
            Intent main = new Intent(SplashScreen.this, MainWindow.class);
            SplashScreen.this.startActivity(main);
            SplashScreen.this.finish();
         }
    });

}

すべてが正しくトリガーされますが(onSuccess、onFinish)、ProgressBarがフリーズし続けます(別のスレッドで実行しようとしても...)。

誰かアイデアがありますか?

4

1 に答える 1

2

ついに動作しました!画像の読み込みとキャッシュにAndroidQuery(ListViewの例)を使用しました(非常にうまく機能します)。

まず、アクティビティA(私のSplashScreenアクティビティ)で静的変数を作成します

protected static ArrayList<HashMap<String, String>> allNews = new ArrayList<HashMap<String, String>>();
protected static ArrayList<JSONObject> items = new ArrayList<JSONObject>();

次に、AsyncTaskを使用して、必要なすべてのJSON情報を取得し、両方のArrayListを入力し、onPostExecuteで他のアクティビティ(私の場合はMainWindow)を作成します。

    @Override
    protected Integer doInBackground(String[]... params) {



        try
        {
            String address = "http://your.file.here";

            HttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost(address);

            try
            {

                HttpResponse response = client.execute(post);
                Log.w("RESPONSE", response.toString());

                BufferedReader reader = new BufferedReader(new InputStreamReader(
                        response.getEntity().getContent(), "UTF-8"));
                String sResponse;

                while ((sResponse = reader.readLine()) != null) {
                    result = result.append(sResponse);
                }

                reader.close();

                JSONArray news = new JSONArray(result.toString());
                int count = news.length();

                for(int i=0; i<news.length(); i++)
                {
                    JSONObject jo = (JSONObject) news.get(i);
                    joNews.add(jo);
                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put("POST_URL", jo.getString("POST_URL"));
                    map.put("TOPIC_TITLE", jo.getString("TOPIC_TITLE"));
                    map.put("AUTHOR", jo.getString("AUTHOR"));
                    map.put("CATEGORY", jo.getString("CATEGORY"));
                    map.put("POST_TIME", jo.getString("POST_TIME"));
                    map.put("POST_TEXT", jo.getString("POST_TEXT"));
                    map.put("IMAGE", jo.getString("IMAGE"));

                    allNews.add(map);
                }
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return 1;
    }

     protected void onPostExecute(Integer result) {
         Intent main = new Intent(SplashScreen.this, MainWindow.class);
         SplashScreen.this.startActivity(main);
         SplashScreen.this.finish();
     }

最後に、アクティビティB(私のMainWindowアクティビティ)の変数を初期化し、AndroidQueryを使用してそれらをListViewにロードします。

private ArrayList<HashMap<String, String>> allNews = new ArrayList<HashMap<String, String>>();
private ArrayList<JSONObject> items = new ArrayList<JSONObject>();

    allNews = SplashScreen.allNews;
    items = SplashScreen.joNews;

    ArrayAdapter<JSONObject> aa = new ArrayAdapter<JSONObject>(this, R.layout.main_news_items, items){

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

                if(convertView == null){
                        convertView = getLayoutInflater().inflate(R.layout.main_news_items, null);
                }

                JSONObject jo = getItem(position);

                AQuery aq = new AQuery(convertView);
                aq.id(R.id.name).text(jo.optString("TOPIC_TITLE", "No Title"));
                aq.id(R.id.title).text(jo.optString("CATEGORY", ""));

                String tb = jo.optString("IMAGE");
                if(tb.equals(""))
                    tb = "http://default.image.here";
                aq.id(R.id.profile_img).progress(R.id.progress).image(tb, true, true, 0, 0, null, AQuery.FADE_IN_NETWORK, 1.0f);


                return convertView;

        }
    };

    ListView news = (ListView) findViewById(R.id.listView1);
    news.setAdapter(aa);
    news.setTextFilterEnabled(true);

これが誰かに役立つことを願っています!

于 2012-12-25T12:37:47.253 に答える