我在android开发中有点新,我试图为应用程序做一个登录过程,我已经成功地在我的本地主机上的一个php脚本上使用http post方法完成了这个过程,但是为了让我的登录过程更好,我决定使用progressdialog来显示什么时候身份验证过程运行,但进度不断循环。下面是我的代码:
LoginActivity.java - 登录按钮:
opt = new HttpRequest();
btnLogin = (Button)findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
v.setEnabled(false);
AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
private boolean authResult = false;
@Override
protected void onPreExecute() {
pd = new ProgressDialog(context);
pd.setTitle("Processing...");
pd.setMessage("Please wait.");
pd.setCancelable(false);
pd.setIndeterminate(true);
pd.show();
}
@Override
protected Void doInBackground(Void... arg0) {
try {
//Do something...
if(opt.loginAuth(txtUsername.getText().toString(), txtPassword.getText().toString())){
Log.i(Data.tagInfo,"Redirecting to mainpage");
opt.showToast(LoginActivity.this,"Login Successful");
startActivity(new Intent("com.example.MainActivity"));
}
else{
opt.showToast(LoginActivity.this,"Login Fail");
Log.i(Data.tagError,"Login Fail");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
return null;
}
@Override
protected void onPostExecute(Void result) {
if (pd!=null) {
pd.dismiss();
btnLogin.setEnabled(true);
}
}
};
task.execute((Void[])null);
}
});
HttpRequest.java:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.ExecutionException;
import org.json.JSONArray;
import android.os.AsyncTask;
import android.util.Log;
public class HttpRequest extends Operations {
public boolean loginAuth(String user , String pass){
//Log.i("Info",user+"|"+pass);
String folder = "mobileapp",
script = "/loginAuth_m-script.php",
fullpath = folder+script,
parameter = "username="+user+"&password="+pass,
result = "";
try {
JSONArray jArray = convertJSON(new SendRequest().execute(fullpath,parameter).get());
result = getSpecificJSONdata(jArray,"result");
Log.i(Data.tagInfo,"Login result:"+result);
if(result.equals(Data.success)){
Log.i(Data.tagInfo,"loginAuth() return true");
return true;
}
else{
Log.i(Data.tagInfo,"loginAuth() return false");
return false;
}
} catch (Exception e) {
// TODO Auto-generated catch block
Log.i(Data.tagError,"Error:"+e.toString());
return false;
}
}
private class SendRequest extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... arg) {
// TODO Auto-generated method stub
HttpURLConnection connection;
OutputStreamWriter request = null;
URL url = null;
String response = null;
try
{
url = new URL("http://"+Data.host+"/"+arg[0]);
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestMethod("POST");
request = new OutputStreamWriter(connection.getOutputStream());
request.write(arg[1]);
request.flush();
request.close();
String line = "";
InputStreamReader isr = new InputStreamReader(connection.getInputStream());
BufferedReader reader = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
// Response from server after login process will be stored in response variable.
response = sb.toString();
// You can perform UI operations here
Log.i("Info","Server Response:"+response);
isr.close();
reader.close();
return response;
}
catch(IOException e)
{
Log.i("InfoError",e.toString());
return "";
// Error
}
}
}
}
提前非常感谢^^