没有输入输入时,应用程序停止

时间:2015-07-01 08:04:56

标签: android login

我遵循了关于将android应用程序连接到外部数据库的教程。下面是检查用户名和密码是否包含在数据库中的代码。但是,当我在两个字段上都没有输入时,应用程序停止。我该怎么办?我是编程新手。

的login.php     

//load and connect to MySQL database stuff

require 'config.inc.php';

if(!empty($_POST)) {

//gets user's info based off of a username.

$query = "
        SELECT
        id,
        username,
        password
        FROM users WHERE username = :username
        ";


$query_params = array(':username' => $_POST['username']);


try {

    $stmt   = $db->prepare($query);
    $result = $stmt->execute($query_params);

}

catch (PDOException $e) {

    // For testing, you could use a die and message.
    //die("Failed to run query: " . $ex->getMessage());
    //or just use this use this one to product JSON data:
    $response["success"] = 0;
    $response["message"] = "Database Error1. Please Try Again!";
    die(json_encode($response));



}

    //This will be the variable to determine whether or not the user's     information is correct.
//we initialize it as false.
$validated_info = false;
//fetching all the rows from the query
$row = $stmt->fetch();
if ($row) {
    //if we encrypted the password, we would unencrypt it here, but in our case we just
    //compare the two passwords
    if ($_POST['password'] === $row['password']) {
        $login_ok = true;
    }
    else
       {$login_ok = false;} 
}


    // If the user logged in successfully, then we send them to the private members-only page
    // Otherwise, we display a login failed message and show the login form again
if ($login_ok) {
    $response["success"] = 1;
    $response["message"] = "Login successful!";
    die(json_encode($response));
} else {

    $response["success"] = 0;
    $response["message"] = "Invalid Credentials!";
    die(json_encode($response));
}

} else {
?>
    <h1>Login</h1> 
    <form action="login.php" method="post"> 
        Username:
        <input type="text" name="username" placeholder="username" /> 
        Password: 
        <input type="password" name="password" placeholder="password" value="" /> 

        <input type="submit" value="Login" /> 
    </form> 

<?php
}

?> 

jsonparser.java

   import java.io.BufferedReader;
   import java.io.IOException;
   import java.io.InputStream;
   import java.io.InputStreamReader;
   import java.io.UnsupportedEncodingException;
   import java.util.List;
   import org.apache.http.HttpEntity;
   import org.apache.http.HttpResponse;
   import org.apache.http.NameValuePair;
   import org.apache.http.client.ClientProtocolException;
   import org.apache.http.client.entity.UrlEncodedFormEntity;
   import org.apache.http.client.methods.HttpGet;
   import org.apache.http.client.methods.HttpPost;
   import org.apache.http.client.utils.URLEncodedUtils;
   import org.apache.http.impl.client.DefaultHttpClient;
   import org.json.JSONException;
   import org.json.JSONObject;

   import android.util.Log;

   public class JSONParser {

static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

// constructor
public JSONParser() {

}


public JSONObject getJSONFromUrl(final String url) {

    // Making HTTP request
    try {
        // Construct the client and the HTTP request.
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        // Execute the POST request and store the response locally.
        HttpResponse httpResponse = httpClient.execute(httpPost);
        // Extract data from the response.
        HttpEntity httpEntity = httpResponse.getEntity();
        // Open an inputStream with the data content.
        is = httpEntity.getContent();

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        // Create a BufferedReader to parse through the inputStream.
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        // Declare a string builder to help with the parsing.
        StringBuilder sb = new StringBuilder();
        // Declare a string to store the JSON object data in string form.
        String line = null;

        // Build the string until null.
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }

        // Close the input stream.
        is.close();
        // Convert the string builder data to an actual string.
        json = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // Try to parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // Return the JSON Object.
    return jObj;

}


// function get json from url
// by making HTTP POST or GET mehtod
public JSONObject makeHttpRequest(String url, String method,
                                  List<NameValuePair> params) {

    // Making HTTP request
    try {

        // check for request method
        if(method.equalsIgnoreCase("POST")){
            // request method is POST
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(params));

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        }else if(method.equalsIgnoreCase("GET")){
            // request method is GET
            DefaultHttpClient httpClient = new DefaultHttpClient();
            String paramString = URLEncodedUtils.format(params, "utf-8");
            url += "?" + paramString;
            HttpGet httpGet = new HttpGet(url);

            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
        }

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;

}
   }

login.java

    import android.app.ProgressDialog;
    import android.content.Context;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.support.v7.app.ActionBarActivity;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.text.InputType;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.MotionEvent;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ImageView;
    import android.widget.Toast;

    import org.apache.http.NameValuePair;
    import org.apache.http.message.BasicNameValuePair;
    import org.json.JSONException;
    import org.json.JSONObject;

    import java.util.ArrayList;
    import java.util.List;
    import android.os.AsyncTask;


    public class Login extends AppCompatActivity {

public static final String MyPREFERENCES = "MyPrefs" ;
public static final String Name = "nameKey";

SharedPreferences sharedpreferences;


       EditText et_username, et_password;
Button btnlogin;
ImageView pass_visible;

private ProgressDialog pDialog;

JSONParser jsonParser = new JSONParser();

private static final String LOGIN_URL =
"http://192.xx.xxx.xxx/webservice_phrism/login.php";


private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
       public static String filename = "MySharedString";

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

    sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);


    et_username = (EditText) findViewById(R.id.username);
    et_password = (EditText) findViewById(R.id.password);
    btnlogin = (Button) findViewById(R.id.login_btn);
    pass_visible = (ImageView) findViewById(R.id.img_visible);

    btnlogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new AttemptLogin().execute();
        }
    });

    pass_visible.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                   et_password.setInputType(InputType.TYPE_CLASS_TEXT);
                    break;
                case MotionEvent.ACTION_UP:
                    et_password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    break;
            }
            return true;
        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_login, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
class AttemptLogin extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread Show Progress Dialog
     * */
    boolean failure = false;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(Login.this);
        pDialog.setMessage("Attempting login...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    @Override
    protected String doInBackground(String... args) {
        // TODO Auto-generated method stub
        // Check for success tag
        int success;
        String username = et_username.getText().toString();
        String password = et_password.getText().toString();
        try {
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("username", username));
            params.add(new BasicNameValuePair("password", password));

            Log.d("request!", "starting");
            // getting product details by making HTTP request
            JSONObject json = jsonParser.makeHttpRequest(
                    LOGIN_URL, "POST", params);

            // check your log for json response
            Log.d("Login attempt", json.toString());

            // json success tag
            success = json.getInt(TAG_SUCCESS);
            if (success == 1) {
                Log.d("Login Successful!", json.toString());
                SharedPreferences.Editor editor = sharedpreferences.edit();
                editor.putString(Name,username);
                editor.commit();
                Intent i = new Intent(Login.this, Dashboard.class);

                finish();
                startActivity(i);
                return json.getString(TAG_MESSAGE);

            }else{
                Log.d("Login Failure!", json.getString(TAG_MESSAGE));
                return json.getString(TAG_MESSAGE);

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;

    }
    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) {
        // dismiss the dialog once product deleted
        pDialog.dismiss();
        if (file_url != null){
            Toast.makeText(Login.this, file_url, Toast.LENGTH_LONG).show();
        }

    }

}


    }

2 个答案:

答案 0 :(得分:0)

您应该控制客户端的输入字段。尝试在填写字段之前不显示发送按钮。

或者例如,您可以检查edittext字段是否为空,在这种情况下,您按下按钮并且不要调用发送功能。

编辑:

检查字段是否为空的示例代码。

    if (((EditText)findviewById(R.id.editTextID)).getText().toString().isEmpty()){            
        //Message field is empty
    }

您可以构造一个布尔函数来检查所有字段。当此函数返回true时,激活按钮。或者,在此描述的函数返回true之前,不要调用发送信息函数

答案 1 :(得分:0)

您应该在收到服务器从DoInBackground到onPostExecute的响应后移动代码,并将响应传递给该方法。您无法在doInBackground线程中更改UI的状态,仅在onPostExecute和onPreExecute中更改。  我认为从这个单独的线程开始新的意图会导致问题。

另外,showDialog在调用AsyncTask之前,检查这些edittexts是否实际上是!= null以及你引用它们的布局。当用户单击按钮进行登录时,则收集输入数据并将其传递给异步任务。

因此,当用户单击“登录”按钮,显示“对话框”时,在对话框中单击某个按钮,调用新的AsyncTask并传递用户名和密码字段的值。在doInBackground调用服务器上,当你得到响应时,只需将它传递给onPostExecute并从那里调用new Intent。