Android工作室,登录表单无法连接mysql数据库

时间:2018-02-14 17:50:46

标签: php android mysql

我无法连接到mysql数据库登录,似乎无法找到错误,因为我是初学者。它总是显示OOP!有些不对劲。连接问题。我在AndroidManifest中包含了网络和互联网权限。我不知道它是php还是java

MainActivity.java

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
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;

public class MainActivity extends AppCompatActivity {

public static final int CONNECTION_TIMEOUT = 10000;
public static final int READ_TIMEOUT = 15000;
private EditText etName;
private EditText etPassword;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    etName = (EditText) findViewById(R.id.username);
    etPassword = (EditText) findViewById(R.id.password);
}

/*public void openSales(View view){
    Intent i = new Intent(this, menuActivity.class);
    startActivity(i);
}*/
public void checkLogin(View arg0) {

    final String name = etName.getText().toString();
    final String password = etPassword.getText().toString();
    new AsyncLogin().execute(name,password);
}
private class AsyncLogin extends AsyncTask<String, String, String> {
    ProgressDialog pdLoading = new ProgressDialog(MainActivity.this);
    HttpURLConnection conn;
    URL url = null;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pdLoading.setMessage("\tLoading...");
        pdLoading.setCancelable(false);
        pdLoading.show();
    }

    @Override
    protected String doInBackground(String... params) {
        try {
            url = new URL("http://localhost/THESIS/database.inc.php");
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return "exception";
        }
        try {
            conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(READ_TIMEOUT);
            conn.setConnectTimeout(CONNECTION_TIMEOUT);
            conn.setRequestMethod("POST");
            conn.setDoInput(true);
            conn.setDoOutput(true);
            Uri.Builder builder = new Uri.Builder()
                    .appendQueryParameter("username", params[0])
                    .appendQueryParameter("password", params[1]);
            String query = builder.build().getEncodedQuery();
            OutputStream os = conn.getOutputStream();
            BufferedWriter writer = new BufferedWriter(
                    new OutputStreamWriter(os, "UTF-8"));
            writer.write(query);
            writer.flush();
            writer.close();
            os.close();
            conn.connect();

        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
            return "exception";
        }
        try {

            int response_code = conn.getResponseCode();
            if (response_code == HttpURLConnection.HTTP_OK) {
                InputStream input = conn.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(input));
                StringBuilder result = new StringBuilder();
                String line;

                while ((line = reader.readLine()) != null) {
                    result.append(line);
                }
                return (result.toString());

            } else {

                return ("unsuccessful");
            }

        } catch (IOException e) {
            e.printStackTrace();
            return "exception";
        } finally {
            conn.disconnect();
        }


    }

    @Override
    protected void onPostExecute(String result) {
        pdLoading.dismiss();

        if (result.equalsIgnoreCase("true")) {
            Intent intent = new Intent(MainActivity.this, menuActivity.class);
            startActivity(intent);
            MainActivity.this.finish();

        } else if (result.equalsIgnoreCase("false")) {
            Toast.makeText(MainActivity.this, "Invalid email or password", Toast.LENGTH_LONG).show();

        } else if (result.equalsIgnoreCase("exception") || result.equalsIgnoreCase("unsuccessful")) {

            Toast.makeText(MainActivity.this, "OOPs! Something went wrong. Connection Problem.", Toast.LENGTH_LONG).show();
        }
    }
}
}

这是php文件

$servername = "db4free.net:3307/******";
$username = "*****";
$password = "*********";
$dbname = "*******";

try {[enter image description here][1]
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
    die("OOPs something went wrong");
}
   if(isset($_POST['username']) && isset($_POST['password']))
 {
      // Innitialize Variable
      $result='';
      $username = $_POST['username'];
      $password = $_POST['password'];

      // Query database for row exist or not
      $sql = 'SELECT * FROM asd WHERE  name = :username AND pass = :password';
      $stmt = $conn->prepare($sql);
      $stmt->bindParam(':username', $username, PDO::PARAM_STR);
      $stmt->bindParam(':password', $password, PDO::PARAM_STR);
      $stmt->execute();
      if($stmt->rowCount())
      {
         $result="true";    
      }  
      elseif(!$stmt->rowCount())
      {
            $result="false";
      }

      // send result back to android
      echo $result;
}  

这是事件日志的img TIA

1 个答案:

答案 0 :(得分:1)

Array.IndexOf()更改为localhost。更多解释here