Android 如何从AsyncTask获取int值
我想获取状态值GetLoginDetails()方法..但我不知道如何操作。 有人能帮我做这件事吗?下面是我完成的工作。。 LoginScreen.javaAndroid 如何从AsyncTask获取int值,android,json,android-asynctask,Android,Json,Android Asynctask,我想获取状态值GetLoginDetails()方法..但我不知道如何操作。 有人能帮我做这件事吗?下面是我完成的工作。。 LoginScreen.java public class LoginScreen extends Activity implements OnClickListener{ EditText etusername,etpassword; public static Integer status= -1;
public class LoginScreen extends Activity implements OnClickListener{
EditText etusername,etpassword;
public static Integer status= -1;
private ProgressDialog pDialog;
// URL to get contacts JSON
private static String url= "http://xxx.xxx.x.xxx/abc/login.php";
private static final String LOGIN = "Login";
private static final String STATUS = "Status";
// contacts JSONArray
JSONArray loginjsonarray=null;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_screen);
initialize();
}
//Initialization of components
private void initialize() {
//Getting the reference of font from assets folder
String fontPath = "Font/Arsenal-Regular.otf";
Typeface tf = Typeface.createFromAsset(getAssets(),fontPath);
//Getting the reference of EditText
etusername=(EditText)findViewById(R.id.editTextLoginusernamenumber);
etpassword=(EditText)findViewById(R.id.editTextLoginpassword);
//Getting the reference of textView
TextView textViewloginforgotpassword=(TextView)findViewById(R.id.textViewloginforgotpassword);
//set font to textView
textViewloginforgotpassword.setTypeface(tf);
//set underline TextView
textViewloginforgotpassword.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);
//Getting references of buttons
Button buttonlogin=(Button)findViewById(R.id.buttonlogin);
//set on click listener on buttons
buttonlogin.setOnClickListener(this);
}//end of Initialization
//on click method
@Override
public void onClick(View v) {
if(v.getId()==R.id.buttonlogin){
new GetLoginDetails().execute();
if(status==1)**//here I want to check that value**
{
Intent intent=new Intent(LoginScreen.this,MenuScreen.class);
startActivity(intent);
finish();
}
}
}//end of on click
@Override
public void onBackPressed(){
finish();
super.onBackPressed();
}
private class GetLoginDetails extends AsyncTask<Void, Void, Integer>
{
protected void onPreExecute() {
// Showing progress dialog
pDialog = new ProgressDialog(LoginScreen.this);
pDialog.setMessage("Loading...");
pDialog.setCancelable(false);
pDialog.show();
}
protected Integer doInBackground(Integer... arg) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username",etusername.getText().toString()));
params.add(new BasicNameValuePair("userPassword", etpassword.getText().toString()));
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonstr = sh.makeServiceCall(url, ServiceHandler.GET, params);
Log.d("Response: ", ">"+jsonstr);
if(jsonstr!=null){
try {
JSONObject jsonObj =new JSONObject(jsonstr);
loginjsonarray=jsonObj.getJSONArray(LOGIN);
for(int i=0;i<loginjsonarray.length();i++){
JSONObject l=loginjsonarray.getJSONObject(i);
status=l.getInt(STATUS);**//how to get this value to login_button onclick scope??**
}
} catch (JSONException e) {
e.printStackTrace();
}
}else{
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return status;
}
protected void onPostExecute(Integer result) {
// Dismiss the progress dialog
if(pDialog.isShowing()){
pDialog.dismiss();
}
return result;
}
}
}
公共类LoginScreen扩展活动实现OnClickListener{
编辑文本etusername,etpassword;
公共静态整数状态=-1;
私人对话;
//获取联系人JSON的URL
专用静态字符串url=”http://xxx.xxx.x.xxx/abc/login.php";
私有静态最终字符串LOGIN=“LOGIN”;
私有静态最终字符串STATUS=“STATUS”;
//联系JSONArray
JSONArray loginjsonarray=null;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u login\u屏幕);
初始化();
}
//组件的初始化
私有void初始化(){
//从资产文件夹中获取字体的引用
字符串fontPath=“Font/arsen Regular.otf”;
Typeface tf=Typeface.createFromAsset(getAssets(),fontPath);
//获取EditText的引用
etusername=(EditText)findViewById(R.id.editTextLoginUserName);
etpassword=(EditText)findViewById(R.id.editTextLoginpassword);
//获取textView的引用
TextView textViewloginforgotpassword=(TextView)findViewById(R.id.textViewloginforgotpassword);
//将字体设置为textView
textViewloginforgotpassword.setTypeface(tf);
//设置下划线文本视图
textViewloginforgotpassword.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);
//获取按钮的引用
Button buttonlogin=(Button)findViewById(R.id.buttonlogin);
//单击按钮设置侦听器
buttonlogin.setOnClickListener(此);
}//初始化结束
//点击法
@凌驾
公共void onClick(视图v){
if(v.getId()==R.id.buttonlogin){
新建GetLoginDetails().execute();
if(status==1)**//这里我想检查这个值**
{
Intent Intent=newintent(LoginScreen.this,MenuScreen.class);
星触觉(意向);
完成();
}
}
}//点击结束
@凌驾
public void onBackPressed(){
完成();
super.onBackPressed();
}
私有类GetLoginDetails扩展了AsyncTask
{
受保护的void onPreExecute(){
//显示进度对话框
pDialog=newprogressdialog(LoginScreen.this);
设置消息(“加载…”);
pDialog.setCancelable(假);
pDialog.show();
}
受保护的整数doInBackground(整数…arg){
List params=new ArrayList();
add(新的BasicNameValuePair(“username”,etusername.getText().toString());
add(新的BasicNameValuePair(“userPassword”,etpassword.getText().toString());
//创建服务处理程序类实例
ServiceHandler sh=新的ServiceHandler();
//向url发出请求并获得响应
字符串jsonstr=sh.makeServiceCall(url,ServiceHandler.GET,params);
Log.d(“响应:”、“>”+jsonstr);
if(jsonstr!=null){
试一试{
JSONObject jsonObj=新的JSONObject(jsonstr);
loginjsonarray=jsonObj.getJSONArray(LOGIN);
对于onPostExecute中的(int i=0;i)
在//之前,关闭进度对话框
//compare your status/integer value over here and depending on that, while dismissing the dialog
if(pDialog.isShowing() && compareValue()){
pDialog.dismiss();
//show a custom dialog maybe or depending on your requirement carry on with your implementation
} else {
//show the message to the user (not signed in or invalid credentials dialog etc)
}
您有几个选择:
- 使用
处理程序
。您可以在活动
中定义它,通过一个新函数(如setHandler(Handler my_Handler)
将它传递给异步任务
,并从中发送消息
- 使用本地的
广播接收器
。如果你不知道怎么做,这是一个很好的例子,并且解释得很好
----编辑---
这是处理程序的一个示例。在活动中定义如下内容:
public class MyRcvData extends Handler {
@Override
public void handleMessage(Message msg) {
Integer your_int = msg.arg1;
Log.d("MyClass", "My Integer is: " + your_int);
}
}
Message msg = Message.obtain();
msg.arg1 = your_integer;
msg.sendMessage();
定义该处理程序的实例。将其另存为类范围的实例,以便在需要时访问:
public class LoginScreen extends Activity implements OnClickListener {
... Declarations ...
MyRcvData myHandler = new MyRcvData();
在AsyncTask
中,添加一个public方法来设置处理程序,以便AsyncTask
知道使用哪个处理程序发送消息
public void setHandler(Handler h) { myHandler = h; }
当然,这意味着您还需要将处理程序
存储在异步任务
中
您必须在调用execute()
之前调用此方法,因此您的MyRcvData
也必须在调用execute()
之前初始化
现在,您只需发送“消息”。在异步任务
中,每当您需要将整数
发送到活动
时,您都会执行以下操作:
public class MyRcvData extends Handler {
@Override
public void handleMessage(Message msg) {
Integer your_int = msg.arg1;
Log.d("MyClass", "My Integer is: " + your_int);
}
}
Message msg = Message.obtain();
msg.arg1 = your_integer;
msg.sendMessage();
这将触发上面的handleMessage()
。我强烈建议您查看和以了解更多信息。是的,那么您为什么要编写“如果(状态)…”单独检查。检查条件,或者关闭pd或启动菜单…抱歉,但如果你能帮我举个例子就太好了,因为我是初学者,不容易理解