AsyncTask似乎没有进入onPre / PostExecute()

时间:2018-03-25 08:48:37

标签: android android-asynctask

我有这个类WelcomeActivity和backgroundLogin,它执行登录序列。我在WelcomeActivity上有我的用户名和密码编辑框,然后按下登录按钮我继续到backgroungLogin连接到数据库,然后比较可用条目的值。

WelcomeActivity:

package com.a000webhostapp.cbhtermitecontrol.termitecontrol;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class WelcomeActivity extends AppCompatActivity{

Button loginButton;
EditText userText, codeText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_welcome);

    loginButton = (Button) findViewById(R.id.loginButtonXML);

    userText = (EditText) findViewById(R.id.userTextXML);
    codeText = (EditText) findViewById(R.id.codeTextXML);

    loginButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
                    String username     = userText.getText().toString();
                    String password     = codeText.getText().toString();
                    String type = "login";
                    backgroundLogin backgroundLogin = new backgroundLogin(WelcomeActivity.this);
                    backgroundLogin.execute(type, username, password);
        }
    });

}

public void onLogin(View view){
    String username     = userText.getText().toString();
    String password     = codeText.getText().toString();
    String type = "login";
    backgroundLogin backgroundLogin = new backgroundLogin(this);
    backgroundLogin.execute(type, username, password);
}

}

backgroundActivity:

package com.a000webhostapp.cbhtermitecontrol.termitecontrol;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.speech.tts.Voice;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

public class backgroundLogin extends AsyncTask<String, Void, String> {
Context context;
AlertDialog alertDialog;
backgroundLogin (Context ctx){
    this.context = ctx;
}

@Override
protected String doInBackground(String... params) {
    String type =   params[0];
    String login_url = "https://cbhtermitecontrol.000webhostapp.com/android/loginA2DB.php";
    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;
            while ((line = bufferedReader.readLine())!=null){
                result+=line;
            }
            bufferedReader.close();
            inputStream.close();
            httpURLConnection.disconnect();
            return result;
        } catch (IOException e) {
            e.printStackTrace();
        }
    return null;
}

@Override
protected void onPreExecute() {
    alertDialog = new AlertDialog.Builder(context).create();
    alertDialog.setTitle("Login Status");
}

@Override
protected void onPostExecute(String result) {
    alertDialog = new AlertDialog.Builder(context).create();
    alertDialog.setTitle("Login Status");
}

@Override
protected void onProgressUpdate(Void... values) {
    super.onProgressUpdate(values);
}

}

有我的代码,但是当我按下登录按钮时,onPre / PostExecute似乎无法正常工作。查看Android App中的运行应用程序日志按下登录按钮后我收到的唯一反馈是

D/libc: [NET] android_getaddrinfo_proxy get netid:0
D/libc: [NET] android_getaddrinfo_proxy-, success

1 个答案:

答案 0 :(得分:0)

请务必致电

alertDialog.show();

onPreExecute和onPostExecute