如何从AsyncTask的int值 [英] how to get int value from AsyncTask
本文介绍了如何从AsyncTask的int值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要得到的状态值GetLoginDetails()method..but我不知道如何做到这一点。
任何一个可以帮助我做到这一点?以下是我所做的工作..
LoginScreen.java
I want to get the Status value GetLoginDetails() method..but i dont know how to do it. Can any one help me to do this? Following is my work done.. LoginScreen.java
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;
}
}
}
ServiceHandler.java
ServiceHandler.java
public class ServiceHandler {
static String response = null;
public final static int GET = 1;
public final static int POST = 2;
public String makeServiceCall(String url, int method) {
return this.makeServiceCall(url, method, null);
}
/**
* Making service call
* @url - url to make request
* @method - http request method
* @params - http request params
* */
public String makeServiceCall(String url, int method, List<NameValuePair> params) {
try {
DefaultHttpClient httpClient=new DefaultHttpClient();
HttpEntity httpEntity=null;
HttpResponse httpResponse=null;
// Checking http request method type
if(method==POST){
HttpPost httpPost=new HttpPost(url);
if(params!=null)
{
//adding post params
httpPost.setEntity(new UrlEncodedFormEntity(params));
}
httpResponse=httpClient.execute(httpPost);
}
else if(method==GET)
{
// appending params to url
if(params!=null)
{
String paramString=URLEncodedUtils.format(params, "utf-8");
url +="?"+paramString;
}
HttpGet httpGet=new HttpGet(url);
httpResponse=httpClient.execute(httpGet);
}
httpEntity=httpResponse.getEntity();
response=EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return response;
}
}
请帮我尽快
推荐答案
在您的onPostExecute ...
In your onPostExecute...
在该辞退//进度对话框
Before the // Dismiss the progress dialog
//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)
}
这篇关于如何从AsyncTask的int值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文