簡體   English   中英

為什么僅在AsyncTask完成后才顯示我的UI?

[英]Why is my UI only shown when AsyncTask has finished?

我在MainActivity放置了一個名為RetrieveHttp的子類,該子類擴展了AsycTask ,該子類應該在后台進行一些處理。

該活動應按以下方式工作:顯示UI,啟動后台任務(檢索URL,將內容解析為String數組),並在AsyncTask完成后在UI上創建Toast。

不幸的是,UI正在等待doInBackground()方法完成的任務。 僅當AsyncTask完成后,才會顯示UI,而用戶僅會看到黑屏。 您能給我一些建議嗎,我的代碼有什么問題?

public class Splashscreen extends Activity implements OnClickListener {
private String questions[];
//HTTP-Downloader to InputStream
private RetrieveHttp myHttp = new RetrieveHttp();

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

    //..implementing some listeners here, referencing GUI elements

    try {
        questions = myHttp.execute("http://myurl.de").get();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }   
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.hauptmenue, menu);     
    return true;
}

public void onClick(View v) {
    //doing some stuff...
}

public class RetrieveHttp extends AsyncTask<String, Void, String[]> {

    protected void onPreExecute() {
    }

    @Override
    protected String[] doInBackground(String... params) {
        URL url;
        String string = "";
        String[] questions = null;
        InputStream content = null;
        try {
            url = new URL(params[0]);
            content = getInputStreamFromUrl(url.toString());
            try {
                // Stream to String
                string = CharStreams.toString(new InputStreamReader(
                        content, "UTF-8"));
                questions = string.split("#");
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return questions;
    }

    protected void onPostExecute(String[] string) {
        Toast.makeText(getApplicationContext(), 
                    "Finished", Toast.LENGTH_LONG).show();
        return;
    }

}

public static InputStream getInputStreamFromUrl(String url) {
    InputStream content = null;
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response = httpclient.execute(new HttpGet(url));
        content = response.getEntity().getContent();
    } catch (Exception e) {
        Log.e("[GET REQUEST]", "Network exception", e);
    }


    return content;
}
}

因為這條線

questions = myHttp.execute("http://myurl.de").get();

get方法等待asynctask完成,實質上是否定了異步任務應該執行的操作。

刪除get並在onPostExecute的onPostExecute中設置您的questions ,UI會像平常一樣顯示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM