onPostExecute方法没有运行

时间:2016-04-15 21:03:00

标签: php android

问题是onPostExecute方法实际上没有运行,代码在执行onPreExecute后停止。输入凭据后,单击登录按钮它只显示“登录状态”,但不显示结果即“登录成功”或“登录不成功”成功“来自php文件,或者你可以说onPostExecute方法返回null String值。请帮助。我没有得到这个。

 public class BackgroundWorker extends AsyncTask<String,Void,String> {
        Context context;
        AlertDialog alertDialog;
        BackgroundWorker (Context ctx) {
            context = ctx;
        }
        @Override
        protected String doInBackground(String... params) {
            String type = params[0];
            String login_url = "http://127.0.0.1/login3.php";
            if(type.equals("login")) {
                try {
                    String user_name = 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("user_name","UTF-8")+"="+URLEncoder.encode(user_name,"UTF-8")+"&"
                            +URLEncoder.encode("passwords","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 (MalformedURLException e) {
                    e.printStackTrace();
                } 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.setMessage(result);
            alertDialog.show();
        }

    }


PHP FILE USED FOR CONNECTION-->

<?PHP
require "conn.php";
$user_name=$_POST["user_name"];
$user_pass=$_POST["passwords"];
$mysql_qry="select * from login_db where email_id like '$user_name' and password like '$user_pass';";
$result=mysqli_query($conn,$mysql_qry);

if(mysqli_num_rows($result)>0)
{echo "Login success";}

else
    {echo "Login not success";}

?>

1 个答案:

答案 0 :(得分:-1)

要在genymotion模拟器上运行它,请使用url ---&gt;

String login_url = `"http://10.0.3.2:8080/login3.php";`

对于Android设备使用---&gt;`

String login_url = "http://192.168.*.*/login3.php";`