如何从AsyncTask类返回值? [英] How to return value from AsyncTask Class?

查看:209
本文介绍了如何从AsyncTask类返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在onPostExecute中获得result布尔值。有没有办法让结果值0,1成功或不在OK按钮中使用。有办法吗?



  //   AsyncTask类//  
受保护 class 检查 extends AsyncTask< String,JSONObject,Boolean> {
String cc = null;

@覆盖
protected 布尔值doInBackground( String ... params){
RestAPI api = new RestAPI();
boolean codeAuth = false;
尝试 {
JSONObject jsonObj = api.CodeAuthentication(params [ 0 ]) ;
JSONParser解析器= JSONParser();
codeAuth = parser.parseCodeAuth(jsonObj);
cc = params [ 0 ];
} catch (例外e){
Log.d( 检查,e.getMessage());
}
return codeAuth;
}

@覆盖
受保护 void onPostExecute(布尔结果){ // *** HERE
if (result){
Toast.makeText(getApplicationContext(), 成功!!,Toast.LENGTH_HORT).show();
}
else {
Toast.makeText(getApplicationContext(), 错误!!,Toast.LENGTH_SHORT).show();
}
}
}

// 按钮//
public void ok(查看视图){

}





非常感谢

解决方案

我认为,你无法从asynctask实例获得返回值,但有很多方法可以根据asynctask的ret值处理最后的性能。



非常简单的方法是使用回调。请看这里。

http://y-sugasawa.blogspot.jp/2012 /08/androidasynctask.html [ ^ ]

http://android.keicode.com/basics/async- asynctask.php [ ^ ]

I can't get "result"boolean value in onPostExecute. there's way to get result value "0,1" success or not to use in OK button. There's the way?.

//AsyncTask Class//
protected class Check extends AsyncTask<String, JSONObject, Boolean> { 
		String cc=null;
		
		@Override
		protected Boolean doInBackground(String... params) {
			RestAPI api = new RestAPI();
			boolean codeAuth = false;
			try {
				JSONObject jsonObj = api.CodeAuthentication(params[0]);
				JSONParser parser = new JSONParser();
				codeAuth = parser.parseCodeAuth(jsonObj);
				cc=params[0];	
			} catch (Exception e) {
				Log.d("Check", e.getMessage());
			}
			return codeAuth;
		}

		@Override
		protected void onPostExecute(Boolean result) {//***HERE
			if (result) {
				Toast.makeText(getApplicationContext(), "Success!!",Toast.LENGTH_HORT).show();
			}
			else {
				Toast.makeText(getApplicationContext(), "Error!!",Toast.LENGTH_SHORT).show();
			}
		}
}

 //BUTTON//
     public void ok(View view){
        
     }	



Thanks a lots

解决方案

I think, you could not get return value from asynctask instance but there are many ways to process last performance according to ret value of asynctask.

Very simple ways is to use callback. See here.
http://y-sugasawa.blogspot.jp/2012/08/androidasynctask.html[^]
http://android.keicode.com/basics/async-asynctask.php[^]


这篇关于如何从AsyncTask类返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆