我正在创建登录功能,通常用户键入他们的凭据并检查他们的凭据到服务器是否经过身份验证。如果它是正确的,它将重定向到另一个页面,否则它将显示错误消息。所以我希望加载一个进度对话框,以便在服务器和客户端之间进行通信。请你帮助我好吗。这是我的登录按钮onclick功能 这里实际上我尝试了异步任务它正在成功登录,但如果输入错误的用户名密码,我需要显示此message.setText(s);怎么办呢?
@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();
}
}
答案 0 :(得分:1)
请使用下面的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();
}
});
}
}