Java 是否可以运行;异步任务“;“另一种方法中的方法”;异步任务;?

Java 是否可以运行;异步任务“;“另一种方法中的方法”;异步任务;?,java,android,json,android-asynctask,Java,Android,Json,Android Asynctask,我有一个活动,启动时调用“json”获取歌曲的数据类别,然后调用“AsyncTask”方法从另一个“json”获取该类别的歌曲列表。问题是,当我启动该活动时,它在2秒后被锁定,活动打开布局,我可以在操作栏上看到类别,而不是因为歌曲是在后台查找的 主要活动(onCreate): java.io.InputStream source=null; source=retrieveStream(UrlApi.URL\u BASE+UrlApi.URL\u STORE+\u bundle.getString

我有一个活动,启动时调用“json”获取歌曲的数据类别,然后调用“AsyncTask”方法从另一个“json”获取该类别的歌曲列表。问题是,当我启动该活动时,它在2秒后被锁定,活动打开布局,我可以在操作栏上看到类别,而不是因为歌曲是在后台查找的

主要活动(onCreate):

java.io.InputStream source=null;
source=retrieveStream(UrlApi.URL\u BASE+UrlApi.URL\u STORE+\u bundle.getString(“\u id”)+UrlApi.CATEGORY\u SONG);
Log.i(“URL-KARAOKE”,UrlApi.URL\u BASE+UrlApi.URL\u STORE+\u bundle.getString(“\u id”)+UrlApi.CATEGORY\u SONG);
Reader Reader=新的InputStreamReader(源);
类型happyCollection=newTypeToken(){}.getType();
_卡拉OK_category_response=new Gson().fromJson(reader,happyCollection);
如果(_karaoke_category_response.size()<1){
完成();
Toast.makeText(getApplicationContext(),“本地单卡拉OK”,Toast.LENGTH_SHORT.show();
}否则{
Log.i(“Category-response”,_karaoke_Category_response.toString());
_卡拉OK \类别\适配器=新的阵列适配器(getSupportActionBar().getThemedContext(),R.layout.spinner \项,\卡拉OK \类别\响应);
getSupportActionBar().setListNavigationCallbacks(_卡拉OK_category_adapter,this);
}
下面的代码用于搜索该类别的歌曲并进行设置

    class AsyncKaraoke extends AsyncTask<Void, Void, Void> {
    String category;

    public AsyncKaraoke(String category) {
        this.category = category;
    }

    protected void onPreExecute(){
        super.onPreExecute();
        setSupportProgressBarIndeterminateVisibility(true);
    }

    @Override
    protected Void doInBackground(Void... params) {
        java.io.InputStream source = null;

        try {
            source = retrieveStream(UrlApi.URL_BASE + UrlApi.URL_STORE + _bundle.getString("_id") + UrlApi.KARAOKE_URL + UrlApi.FILTER_CATEGORY + URLEncoder.encode(category, "UTF-8"));
            Log.i("URL - KARAOKE", UrlApi.URL_BASE + UrlApi.URL_STORE + _bundle.getString("_id") + UrlApi.KARAOKE_URL + UrlApi.FILTER_CATEGORY + URLEncoder.encode(category, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Reader reader = new InputStreamReader(source);
        Type karaokeCollection = new TypeToken<Collection<KaraokeModel>>() {}.getType();
        _response = new Gson().fromJson(reader, karaokeCollection);
        Log.i("Response - KaraokeCategory" , _karaoke_category_response.toString());
        return null;
    }
    protected void onPostExecute(Void Void){
        super.onPostExecute(Void);
        setSupportProgressBarIndeterminateVisibility(false);
        _karaoke_adapter = new KaraokeAdapter(KaraokeActivity.this,  _bundle.getString("_id"), _response);
        if(_response.size() == 0){
            Toast.makeText(getApplicationContext(), "Categoria sin karaoke", Toast.LENGTH_SHORT).show();
        }
        _list_view.setAdapter(_karaoke_adapter);
        _karaoke_adapter.notifyDataSetChanged();
    }


}
类AsyncKaraoke扩展了AsyncTask{
字符串类别;
公共卡拉OK(弦乐类){
this.category=类别;
}
受保护的void onPreExecute(){
super.onPreExecute();
SetSupportProgressBarInDeterminateVibility(真);
}
@凌驾
受保护的Void doInBackground(Void…参数){
java.io.InputStream source=null;
试一试{
source=retrieveStream(UrlApi.URL\u BASE+UrlApi.URL\u STORE+\u bundle.getString(“\u id”)+UrlApi.KARAOKE\u URL+UrlApi.FILTER\u CATEGORY+URLEncoder.encode(CATEGORY,“UTF-8”);
Log.i(“URL-KARAOKE”,UrlApi.URL\u BASE+UrlApi.URL\u STORE+\u bundle.getString(“\u id”)+UrlApi.KARAOKE\u URL+UrlApi.FILTER\u CATEGORY+urlacoder.encode(CATEGORY,“UTF-8”);
}捕获(不支持的编码异常e){
e、 printStackTrace();
}
Reader Reader=新的InputStreamReader(源);
类型karaokeCollection=newTypeToken(){}.getType();
_response=new Gson().fromJson(读卡器,卡拉ok集合);
Log.i(“Response-karaokecompose”,_karaoke_category_Response.toString());
返回null;
}
PostExecute上受保护的void(void void){
super.onPostExecute(无效);
SetSupportProgressBarInDeterminateVibility(假);
_卡拉OK_adapter=新的卡拉OK适配器(KaraokeActivity.this,_bundle.getString(“_id”),_response);
如果(_response.size()==0){
Toast.makeText(getApplicationContext(),“卡拉OK分类”,Toast.LENGTH\u SHORT.show();
}
_list_view.setAdapter(_karaoke_adapter);
_卡拉OK_适配器。notifyDataSetChanged();
}
}

我应该如何调用2次“AsyncTask”方法并阻止活动在几秒钟内启动?

AsyncTask的主要规则是它必须始终在主线程上创建和运行。如果在
doInBackground()
方法内启动另一个
AsyncTask
,则会出现异常。您可以选择在其中一个回调中启动下一个
AsyncTask
。通常,有些人会在
onPostExecute()
方法中链接
AsyncTask
,但您也可以在
onPreExecute()
onProgressUpdate()中启动它们

编辑:


此外,可以使用按顺序运行AsyncTask。从蜂巢开始,你不需要这样做。所有异步任务都按照执行顺序在串行线程池中运行。不过,如果您使用它,可能更容易理解代码是串行运行的。如果使用Android 1.6-2.3.x,您确实需要链接。

异步任务的主要规则是它必须始终在主线程上创建和运行。如果在
doInBackground()
方法内启动另一个
AsyncTask
,则会出现异常。您可以选择在其中一个回调中启动下一个
AsyncTask
。通常,有些人会在
onPostExecute()
方法中链接
AsyncTask
,但您也可以在
onPreExecute()
onProgressUpdate()中启动它们

int count = 0;

protected void onPostExecute(Void Void){
        super.onPostExecute(Void);

        // call same asynctask
          if (count == 0)
            {     
                execute asynctask
                count++;
            }
}
编辑:


此外,可以使用按顺序运行AsyncTask。从蜂巢开始,你不需要这样做。所有异步任务都按照执行顺序在串行线程池中运行。不过,如果您使用它,可能更容易理解代码是串行运行的。如果使用Android 1.6-2.3.x,您确实需要链接。

您应该在主活动中构建URL,然后运行AsyncTask下载内容,最后在活动中处理结果

int count = 0;

protected void onPostExecute(Void Void){
        super.onPostExecute(Void);

        // call same asynctask
          if (count == 0)
            {     
                execute asynctask
                count++;
            }
}
运行异步任务的语法为:

String category = "...";
new AsyncKaraoke().execute(category);
您还可以从AsyncKaraoke类中删除onPostExecute()方法,并将其放入活动中:

String category = "...";
new AsyncKaraoke() {
  @Override
  protected void onPostExecute(Void Void){
    // do stuff (and moving the third type of the AsyncKaraoke to something else
    // than Void will allow you to get the result here.
}.execute(category);

您应该在主活动中构建URL,然后运行异步任务下载内容,最后在活动中处理结果

运行异步任务的语法为:

String category = "...";
new AsyncKaraoke().execute(category);
您还可以从AsyncKaraoke类中删除onPostExecute()方法,并将其放入活动中:

String category = "...";
new AsyncKaraoke() {
  @Override
  protected void onPostExecute(Void Void){
    // do stuff (and moving the third type of the AsyncKaraoke to something else
    // than Void will allow you to get the result here.
}.execute(category);

通常,我们使用
AsyncTask
UI
线程之外的另一个
线程中执行操作,以防止用户在执行某些操作时被暂停因此,它不会产生任何错误