Problème de connexion BDD externe
Bonjour,
J'ai désormais le souci suivant : je tente de me connecter à ma BDD (la connexion est OK via le navigateur), mais je n'arrive pas à afficher ma page d'accueil lors du login.
Malgré mes recherches je ne trouve pas.
Un idée svp ?
Par avance merci
MainActivity
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
| package com.simag.www.connect_test;
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.util.Log;
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 {
// CONNECTION_TIMEOUT and READ_TIMEOUT are in milliseconds
public static final int CONNECTION_TIMEOUT = 10000;
public static final int READ_TIMEOUT = 15000;
private EditText etEmail;
private EditText etPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get Reference to variables
etEmail = (EditText) findViewById(R.id.email);
etPassword = (EditText) findViewById(R.id.password);
}
// Triggers when LOGIN Button clicked
public void checkLogin(View arg0) {
// Get text from email and passord field
final String email = etEmail.getText().toString();
final String password = etPassword.getText().toString();
// Initialize AsyncLogin() class with email and password
new AsyncLogin().execute(email, 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();
//this method will be running on UI thread
pdLoading.setMessage("\tLoading...");
pdLoading.setCancelable(false);
pdLoading.show();
}
@Override
protected String doInBackground(String... params) {
try {
// Enter URL address where your php file resides
url = new URL("http://192.168.56.1/Test/login.php");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "exception";
}
try {
// Setup HttpURLConnection class to send and receive data from php and mysql
conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(READ_TIMEOUT);
conn.setConnectTimeout(CONNECTION_TIMEOUT);
conn.setRequestMethod("POST");
// setDoInput and setDoOutput method depict handling of both send and receive
conn.setDoInput(true);
conn.setDoOutput(true);
// Append parameters to URL
Uri.Builder builder = new Uri.Builder().appendQueryParameter("username", params[0]).appendQueryParameter("password", params[1]);
String query = builder.build().getEncodedQuery();
// Open connection for sending data
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();
// Check if successful connection made
if (response_code == HttpURLConnection.HTTP_OK) {
// Read data sent from server
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);
}
// Pass data to onPostExecute method
return (result.toString());
} else {
return ("unsuccessful");
}
} catch (IOException e) {
e.printStackTrace();
return "exception";
} finally {
conn.disconnect();
}
}
@Override
protected void onPostExecute(String result) {
//this method will be running on UI thread
pdLoading.dismiss();
if (result.equalsIgnoreCase("true")) {
/* Here launching another activity when login successful. If you persist login state
use sharedPreferences of Android. and logout button to clear sharedPreferences.
*/
Intent intent = new Intent(MainActivity.this, com.simag.www.connect_test.SuccessActivity.class);
startActivity(intent);
MainActivity.this.finish();
} else if (result.equalsIgnoreCase("false")) {
// If username and password does not match display a error message
Toast.makeText(MainActivity.this, "User ou Login incorrect", Toast.LENGTH_LONG).show();
} else if (result.equalsIgnoreCase("exception") || result.equalsIgnoreCase("unsuccessful")) {
Toast.makeText(MainActivity.this, "Problème de connection.", Toast.LENGTH_LONG).show();
}
}
}
} |
Manifest
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.simag.www.connect_test" >
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="Login">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SuccessActivity"
android:label="Accueil"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
</application>
</manifest> |
Log.php
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| <?php
include 'logs.php';
// Check whether username or password is set from android
if(isset($_POST['username']) && isset($_POST['password']))
{
// Initialize Variable
$result='';
$username = $_POST['username'];
$password = $_POST['password'];
// Query database for row exist or not
$sql = 'SELECT * FROM users WHERE USER_MAIL = :username AND USER_PSW = :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";
echo $result;
}
elseif(!$stmt->rowCount())
{
$result="false";
}
// send result back to android
echo $result;
}
?> |
Logcat:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| 01-15 09:28:17.255 15438-15438/? I/zygote: Not late-enabling -Xcheck:jni (already on)
01-15 09:28:17.283 15438-15438/? W/zygote: Unexpected CPU variant for X86 using defaults: x86
01-15 09:28:17.473 15438-15438/com.simag.www.connect_test I/InstantRun: starting instant run server: is main process
01-15 09:28:17.474 15438-15438/com.simag.www.connect_test V/InstantRun: Starting server socket listening for package com.simag.www.connect_test on android.net.LocalSocketAddress@d871f87
01-15 09:28:17.474 15438-15438/com.simag.www.connect_test V/InstantRun: Started server for package com.simag.www.connect_test
01-15 09:28:17.955 15438-15456/com.simag.www.connect_test D/OpenGLRenderer: HWUI GL Pipeline
01-15 09:28:18.035 15438-15456/com.simag.www.connect_test I/zygote: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
01-15 09:28:18.035 15438-15456/com.simag.www.connect_test I/OpenGLRenderer: Initialized EGL, version 1.4
01-15 09:28:18.035 15438-15456/com.simag.www.connect_test D/OpenGLRenderer: Swap behavior 1
01-15 09:28:18.035 15438-15456/com.simag.www.connect_test W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
01-15 09:28:18.035 15438-15456/com.simag.www.connect_test D/OpenGLRenderer: Swap behavior 0
01-15 09:28:18.044 15438-15456/com.simag.www.connect_test D/EGL_emulation: eglCreateContext: 0xa24850c0: maj 2 min 0 rcv 2
01-15 09:28:18.107 15438-15456/com.simag.www.connect_test D/EGL_emulation: eglMakeCurrent: 0xa24850c0: ver 2 0 (tinfo 0xa2483330)
01-15 09:28:18.244 15438-15456/com.simag.www.connect_test D/EGL_emulation: eglMakeCurrent: 0xa24850c0: ver 2 0 (tinfo 0xa2483330)
01-15 09:28:18.810 15438-15438/com.simag.www.connect_test V/View: dispatchProvideAutofillStructure(): not laid out, ignoring 0 children of 1073741831
01-15 09:28:18.814 15438-15438/com.simag.www.connect_test I/AssistStructure: Flattened final assist data: 2456 bytes, containing 1 windows, 9 views
01-15 09:28:18.821 15438-15438/com.simag.www.connect_test W/IInputConnectionWrapper: beginBatchEdit on inactive InputConnection
01-15 09:28:18.821 15438-15438/com.simag.www.connect_test W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
01-15 09:28:18.826 15438-15438/com.simag.www.connect_test W/IInputConnectionWrapper: getTextAfterCursor on inactive InputConnection
01-15 09:28:18.826 15438-15438/com.simag.www.connect_test W/IInputConnectionWrapper: getSelectedText on inactive InputConnection
01-15 09:28:18.828 15438-15438/com.simag.www.connect_test W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
01-15 09:28:18.861 15438-15438/com.simag.www.connect_test W/IInputConnectionWrapper: beginBatchEdit on inactive InputConnection
01-15 09:28:18.861 15438-15438/com.simag.www.connect_test W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
01-15 09:28:18.862 15438-15438/com.simag.www.connect_test W/IInputConnectionWrapper: getTextAfterCursor on inactive InputConnection
01-15 09:28:18.863 15438-15438/com.simag.www.connect_test W/IInputConnectionWrapper: getSelectedText on inactive InputConnection
01-15 09:28:18.863 15438-15438/com.simag.www.connect_test W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
01-15 09:28:20.131 15438-15456/com.simag.www.connect_test D/EGL_emulation: eglMakeCurrent: 0xa24850c0: ver 2 0 (tinfo 0xa2483330) |