[英]how to add progress dialog inside button
我正在创建登录功能,通常用户输入其凭据并向服务器检查其凭据是否经过身份验证。 如果正确,它将重定向到另一个页面,否则将显示错误消息。 因此,我希望为服务器和客户端之间的通信放置一个进度对话框。 请你帮助我好吗。 这是我的登录按钮onclick函数,实际上我在尝试异步任务,该任务可成功登录,但是如果输入错误的用户名密码,则需要显示此消息。 怎么办呢?
@SuppressLint("NewApi")
public void onClick(View arg0) {
pd = new ProgressDialog(ParentLogin.this);
pd.setMessage("Please wait...");
pd.show();
new AsyncTask<Void, Void, Void>()
{
@Override
protected Void doInBackground(Void... params)
{
loginformation();
return null;
}
@Override
protected void onPostExecute(Void result)
{
Toast.makeText(getApplicationContext(), "Login Successfully", Toast.LENGTH_SHORT).show();
Intent nextScreen = new Intent(getApplicationContext(), ParentPage.class);
startActivity(nextScreen);
finish();
}
}.execute();
}
public void loginformation(){
un = username.getText().toString();
String pwd = password.getText().toString();
StringBuffer buffer = new StringBuffer();
StringBuffer checkBox2 = buffer.append(checkBox1.isChecked());
String checkBox=checkBox2.toString();
//String checkBox =ms_remember.getText().toString();
if(un.trim().length() > 0 && pwd.trim().length() > 0){
try {
if (currentapiVersion >= android.os.Build.VERSION_CODES.GINGERBREAD){
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
HttpURLConnection urlc = (HttpURLConnection) (new URL("http://www.google.com").openConnection());
urlc.setRequestProperty("User-Agent", "Test");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(15000);
urlc.connect();
if(urlc.getResponseCode() == 200) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(Urllink.url+"parent_login.php?key=agile89rise98&username="+un+"&password="+pwd);
HttpResponse response = null;
try {
response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
//new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String result, line = reader.readLine();
result = line;
while((line = reader.readLine()) != null) {
result += line;
}
s = result;
if(s.equals("1")) {
session.createLoginSession(un);
preference = getSharedPreferences(PREFERENCES, Context.MODE_PRIVATE);
Editor editor1 = preference.edit();
editor1.putString(UName, un);
editor1.putString(Type, "p");
editor1.commit();
//preference activate of check box is enable
if(checkBox.equals("true")) {
Editor editor = sharedpreferences.edit();
editor.putString(UserName, un);
editor.putString(Password, pwd);
editor.commit();
}
if (sharedpreference.contains(GCMID))
{
gcm = sharedpreference.getString(GCMID, "");
if(!prefs.getBoolean("secondTimes", false)) {
//exceute SummaryAsyncTask()
new SummaryAsyncTask().execute((Void) null);
SharedPreferences.Editor editors = prefs.edit();
editors.putBoolean("secondTimes", true);
editors.commit();
}
} else {
if(!prefs.getBoolean("secondTime", false)) {
execute();
SharedPreferences.Editor editors = prefs.edit();
editors.putBoolean("secondTime", true);
editors.commit();
}
}
pd.dismiss;
} else if(s.equals("0")) {
message.setText("Incorrect Username/Passsword");
} else {
pd.dismiss();
message.setText(s);
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
Toast.makeText(getApplicationContext(), "Please Connect Internet", Toast.LENGTH_SHORT).show();
}
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "Please Connect Internet", Toast.LENGTH_SHORT).show();
Log.e(TAG, "Error checking internet connection", e);
}
} else {
Toast.makeText(getApplicationContext(), "Please enter Username & password.", Toast.LENGTH_SHORT).show();
}
}
请使用下面的loginformation方法,您必须使用runOnUithread在doInBackground内部的UI上工作
public void loginformation(){
un = username.getText().toString();
String pwd = password.getText().toString();
StringBuffer buffer = new StringBuffer();
StringBuffer checkBox2 = buffer.append(checkBox1.isChecked());
String checkBox=checkBox2.toString();
//String checkBox =ms_remember.getText().toString();
if(un.trim().length() > 0 && pwd.trim().length() > 0){
try {
if (currentapiVersion >= android.os.Build.VERSION_CODES.GINGERBREAD){
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
HttpURLConnection urlc = (HttpURLConnection) (new URL("http://www.google.com").openConnection());
urlc.setRequestProperty("User-Agent", "Test");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(15000);
urlc.connect();
if(urlc.getResponseCode() == 200) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(Urllink.url+"parent_login.php?key=agile89rise98&username="+un+"&password="+pwd);
HttpResponse response = null;
try {
response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
//new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String result, line = reader.readLine();
result = line;
while((line = reader.readLine()) != null) {
result += line;
}
s = result;
if(s.equals("1")) {
session.createLoginSession(un);
preference = getSharedPreferences(PREFERENCES, Context.MODE_PRIVATE);
Editor editor1 = preference.edit();
editor1.putString(UName, un);
editor1.putString(Type, "p");
editor1.commit();
//preference activate of check box is enable
if(checkBox.equals("true")) {
Editor editor = sharedpreferences.edit();
editor.putString(UserName, un);
editor.putString(Password, pwd);
editor.commit();
}
if (sharedpreference.contains(GCMID))
{
gcm = sharedpreference.getString(GCMID, "");
if(!prefs.getBoolean("secondTimes", false)) {
//exceute SummaryAsyncTask()
new SummaryAsyncTask().execute((Void) null);
SharedPreferences.Editor editors = prefs.edit();
editors.putBoolean("secondTimes", true);
editors.commit();
}
} else {
if(!prefs.getBoolean("secondTime", false)) {
execute();
SharedPreferences.Editor editors = prefs.edit();
editors.putBoolean("secondTime", true);
editors.commit();
}
}
pd.dismiss;
} else if(s.equals("0")) {
runOnUiThread(new Runnable() {
@Override
public void run() {
message.setText("Incorrect Username/Passsword");
}
});
} else {
pd.dismiss();
runOnUiThread(new Runnable() {
@Override
public void run() {
message.setText(s);
}
});
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Please Connect Internet", Toast.LENGTH_SHORT).show();
}
});
}
} catch (IOException e) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Please Connect Internet", Toast.LENGTH_SHORT).show();
}
});
Log.e(TAG, "Error checking internet connection", e);
}
} else {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Please enter Username & password.", Toast.LENGTH_SHORT).show();
}
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.