【发布时间】:2018-01-28 13:47:33
【问题描述】:
我正在登录应用程序。我正在尝试在扩展异步任务的 BackgroundWorker 类中显示 progressDialog。但是只要我点击登录按钮,进度对话框就会显示几秒钟。签名过程是否有可能持续更长时间?
代码片段
`public class BackgrroundWorker extends AsyncTask<String,Void,String>{
Context context;
AlertDialog alertDialog;
boolean correct;
public BackgrroundWorker(Context context){
this.context = context;
}
@Override
protected String doInBackground(String... params) {
String type = params[0];
String login_url = "random";
String register_url = "random";
if(type.equals("login")){
try {
String username = params[1];
String password = params[2];
URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("username","UTF-8") + "=" +URLEncoder.encode(username,"UTF-8")+ "&"
+URLEncoder.encode("password","UTF-8") + "=" +URLEncoder.encode(password,"UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String result = "";
String line = "";
correct = false;
while((line = bufferedReader.readLine()) != null){
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
System.out.println(result);
if (result.equalsIgnoreCase("login success"))
correct=true;
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPreExecute() {
progressDialog = ProgressDialog.show(context, "Login",
"Please wait for a while.", true);
alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("Login status");
}
@Override
protected void onPostExecute(String result) {
if(!result.equals("login success")) {
alertDialog.setMessage(result);
alertDialog.show();
}
progressDialog.dismiss();
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
`
【问题讨论】:
标签: java android android-asynctask progressdialog