MySQL과 Android Studio 응용 프로그램 연결
-
20-12-2019 - |
문제
MySQL 데이터베이스를 사용하여 Android Studio 앱을 연결하려고하지만이 중 일부 문제가 발생합니다.나는 간단한 로그인을했고, 또한 PHP 파일은 DB와 연결을 띠고 있습니다.
public class AnprSdkMain extends Activity implements OnClickListener{
private EditText user, pass;
private Button mSubmit, mRegister;
// Progress Dialog
private ProgressDialog pDialog;
// JSON parser class
JSONParser jsonParser = new JSONParser();
//php login script location:
//localhost :
//testing on your device
//put your local ip instead, on windows, run CMD > ipconfig
//or in mac's terminal type ifconfig and look for the ip under en0 or en1
// private static final String LOGIN_URL = "http://xxx.xxx.x.x:1234/webservice/login.php";
//testing on Emulator:
private static final String LOGIN_URL = "http://127.0.0.1/anpr/webservice/login.php";
//testing from a real server:
//private static final String LOGIN_URL = "http://www.yourdomain.com/webservice/login.php";
//JSON element ids from repsonse of php script:
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//setup input fields
user = (EditText)findViewById(R.id.editText1);
pass = (EditText)findViewById(R.id.editText2);
//setup buttons
mSubmit = (Button)findViewById(R.id.button1);
//register listeners
mSubmit.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
new AttemptLogin().execute();
break;
default:
break;
}
}
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(AnprSdkMain.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 = user.getText().toString();
String password = pass.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());
Intent i = new Intent(AnprSdkMain.this, Lista.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(AnprSdkMain.this, file_url, Toast.LENGTH_LONG).show();
}
}
}
}
.
및 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 $ex) {
// 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;
}
}
// 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:<br />
<input type="text" name="username" placeholder="username" />
<br /><br />
Password:<br />
<input type="password" name="password" placeholder="password" value="" />
<br /><br />
<input type="submit" value="Login" />
</form>
<a href="register.php">Register</a>
<?php
}
.
및 config.inc.php
<?php
// These variables define the connection information for your MySQL database
// This is also for the Xampp example, if you are hosting on your own server,
//make the necessary changes (mybringback_travis, etc.)
$username = "root";
$password = "";
$host = "127.0.0.1";
$dbname = "anprscout";
// UTF-8 is a character encoding scheme that allows you to conveniently store
// a wide varienty of special characters, like ¢ or €, in your database.
// By passing the following $options array to the database connection code we
// are telling the MySQL server that we want to communicate with it using UTF-8
// See Wikipedia for more information on UTF-8:
// http://en.wikipedia.org/wiki/UTF-8
$options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8');
// A try/catch statement is a common method of error handling in object oriented code.
// First, PHP executes the code within the try block. If at any time it encounters an
// error while executing that code, it stops immediately and jumps down to the
// catch block. For more detailed information on exceptions and try/catch blocks:
// http://us2.php.net/manual/en/language.exceptions.php
try
{
// This statement opens a connection to your database using the PDO library
// PDO is designed to provide a flexible interface between PHP and many
// different types of database servers. For more information on PDO:
// http://us2.php.net/manual/en/class.pdo.php
$db = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options);
}
catch(PDOException $ex)
{
// If an error occurs while opening a connection to your database, it will
// be trapped here. The script will output an error and stop executing.
// Note: On a production website, you should not output $ex->getMessage().
// It may provide an attacker with helpful information about your code
// (like your database username and password).
die("Failed to connect to the database: " . $ex->getMessage());
}
// This statement configures PDO to throw an exception when it encounters
// an error. This allows us to use try/catch blocks to trap database errors.
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// This statement configures PDO to return database rows from your database using an associative
// array. This means the array will have string indexes, where the string value
// represents the name of the column in your database.
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
// This block of code is used to undo magic quotes. Magic quotes are a terrible
// feature that was removed from PHP as of PHP 5.4. However, older installations
// of PHP may still have magic quotes enabled and this code is necessary to
// prevent them from causing problems. For more information on magic quotes:
// http://php.net/manual/en/security.magicquotes.php
if(function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc())
{
function undo_magic_quotes_gpc(&$array)
{
foreach($array as &$value)
{
if(is_array($value))
{
undo_magic_quotes_gpc($value);
}
else
{
$value = stripslashes($value);
}
}
}
undo_magic_quotes_gpc($_POST);
undo_magic_quotes_gpc($_GET);
undo_magic_quotes_gpc($_COOKIE);
}
header('Content-Type: text/html; charset=utf-8');
session_start();
.
응용 프로그램을 실행할 때 표시되는 문제점은 다음과 같습니다.
07-28 18:15:34.815 340-347/com.birdorg.anpr.sdk.simple.camera.example E/Buffer Error﹕ Error converting result java.lang.NullPointerException
07-28 18:15:34.815 340-347/com.birdorg.anpr.sdk.simple.camera.example E/JSON Parser﹕ Error parsing data org.json.JSONException: End of input at character 0 of
07-28 18:15:34.825 340-347/com.birdorg.anpr.sdk.simple.camera.example W/dalvikvm﹕ threadid=7: thread exiting with uncaught exception (group=0x4001d800)
07-28 18:15:34.855 340-347/com.birdorg.anpr.sdk.simple.camera.example E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:200)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
at java.lang.Thread.run(Thread.java:1096)
Caused by: java.lang.NullPointerException
at com.birdorg.anpr.sdk.simple.camera.example.AnprSdkMain$AttemptLogin.doInBackground(AnprSdkMain.java:116)
at com.birdorg.anpr.sdk.simple.camera.example.AnprSdkMain$AttemptLogin.doInBackground(AnprSdkMain.java:80)
at android.os.AsyncTask$2.call(AsyncTask.java:185)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
at java.lang.Thread.run(Thread.java:1096)
07-28 18:15:35.025 340-340/com.birdorg.anpr.sdk.simple.camera.example W/IInputConnectionWrapper﹕ showStatusIcon on inactive InputConnection
07-28 18:15:35.724 340-340/com.birdorg.anpr.sdk.simple.camera.example E/WindowManager﹕ Activity com.birdorg.anpr.sdk.simple.camera.example.AnprSdkMain has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4a22d5a0 that was originally added here
android.view.WindowLeaked: Activity com.birdorg.anpr.sdk.simple.camera.example.AnprSdkMain has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4a22d5a0 that was originally added here
at android.view.ViewRoot.<init>(ViewRoot.java:247)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
at android.view.Window$LocalWindowManager.addView(Window.java:424)
at android.app.Dialog.show(Dialog.java:241)
at com.birdorg.anpr.sdk.simple.camera.example.AnprSdkMain$AttemptLogin.onPreExecute(AnprSdkMain.java:94)
at android.os.AsyncTask.execute(AsyncTask.java:391)
at com.birdorg.anpr.sdk.simple.camera.example.AnprSdkMain.onClick(AnprSdkMain.java:73)
at android.view.View.performClick(View.java:2408)
at android.view.View$PerformClick.run(View.java:8816)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
.
Android Studio에서는 새로운 기능입니다.나는 이것에 의해 정말로 도움이 필요합니다
해결책
에뮬레이터를 통해 연결하려는 경우 10.0.2.2
대신 IP 주소 127.0.0.1
사용을 사용하십시오.이 링크 에 대해 더 많이 이야기합니다.
다른 팁
웹 서버와 통신하는 앱을 건설하므로 두 개의 앱 (또는 더 많은)의 통신을 구축하는 것은 컴퓨터 프로그래밍에서 가장 어려운 일 중 하나입니다. 왜냐하면 어떤 측면이 버그가 있는지 아는 것이 어렵 기 때문입니다 (때로는 둘 다 ...)
나는 Great Succes로 사용하는 시작점을 하나의 출발점으로 제안합니다. 프로그래밍, 혼자 PHP 측면으로 시작합니다.매개 변수를 넣는 URL을 사용하는 동안 전화하십시오."echo"가 될 때 웹 브레이더를 통해 PHP를 호출 할 때 결과가 표시됩니다. 이게 괜찮 으면 PHP에 "로그"시스템을 추가하십시오.다음과 같이 :
function debug_log($string_db)
{
// Prepare string with date and time
$the_date = date("[ d/m/Y - H:i:s ] ");
$to_save = $the_date.$string_db."\r"; // and add the string to log
// Open file: write at he the end
$handle = fopen("call.log","a");
// Save string
fwrite($handle,$to_save,strlen($to_save));
fclose($handle); // bye bye
}
.
다음과 같이 사용하십시오 (예 :) : debug_log ( "첫 번째 루프 내부");
매우 "기본"이지만, 이것은 "echo"가 아닌 디스크에 저장됩니다. 이는 Android 앱을 방해하지는 않지만 PHP가 수행하는 것을 살펴볼 수 있습니다.
이를해서 Android Dev로 돌아가서, 이번에는 문제가 발생하면 Android 측면에 있습니다.