Issue
I’m new to Android, in order to prepare for my last school exam, I was asked to put an Android application solution using an external database on phpmyadmin ( wampserver).
My connection is done in the MainActivity file, using OkHttp3 and overriding AsyncTask with a request on an external php file for the authentification.
After a few days of work the connection still doesn’t work and my tests are coming soon, every launch of the android application is in debug mode and here are my error logs when i'm trying to connect :
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: com.example.brobert.biorelai, PID: 6002
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:354)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
at java.util.concurrent.FutureTask.run(FutureTask.java:271)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String okhttp3.Response.toString()' on a null object reference
at com.example.brobert.biorelai.MainActivity$BackTaskAuthentification.doInBackground(MainActivity.java:76)
at com.example.brobert.biorelai.MainActivity$BackTaskAuthentification.doInBackground(MainActivity.java:46)
at android.os.AsyncTask$2.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
I/Process: Sending signal. PID: 6002 SIG: 9
Disconnected from the target VM, address: 'localhost:8600', transport:
'socket'
I already tried to add the internet permission via the AndroidManifest.xml :
<uses-permission android:name="android.permission.INTERNET"/>
To add the okhttp3 dependencies and put the proxy option on auto-detect.
MoreOver with the log function i verified that my EditText was working and my variable request too.
I think the error is on the line 70-71 of my MainActivity file :
response = client.newCall(request).execute();
responseStr = response.body().toString();
My MainActivity File code :
package com.example.brobert.biorelai;
import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View.OnClickListener;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;
import okhttp3.*;
public class MainActivity extends AppCompatActivity {
String responseStr;
OkHttpClient client = new OkHttpClient();
String textLogin1;
String mdp1;
Response response;
RequestBody formBody;
Request request;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button buttonValiderAuthentification = (Button)
findViewById(R.id.button2);
buttonValiderAuthentification.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v){
new BackTaskAuthentification().execute();
}
});
}
private class BackTaskAuthentification extends AsyncTask<Void, Void,
Void> {
@Override
protected void onPreExecute() {
final EditText textLogin = findViewById(R.id.login1);
final EditText textMdp = findViewById(R.id.mdp1);
textLogin1 = textLogin.getText().toString();
mdp1 = textMdp.getText().toString();
}
@Override
protected Void doInBackground(Void... params){
try {
formBody = new FormBody.Builder()
.add("login", textLogin1)
.add("mdp", mdp1)
.build();
request = new Request.Builder()
.url("http://127.0.0.1/bio- relais/controleurMobil/json.php")
.post(formBody)
.build();
response = client.newCall(request).execute();
responseStr = response.body().toString();
} catch (Exception e) {
Log.d("test", textLogin1);
Log.d("test1", mdp1);
Log.d("test3", request.toString());
Log.d("test2", response.toString());
}
return null;
}
@Override
protected void onPostExecute(Void result) {
if (responseStr.compareTo("false") != 0){
try {
JSONObject membre = new JSONObject(responseStr);
String nom = membre.getString("nomM");
Intent intent = new Intent(MainActivity.this,
MainProducteurActivity.class);
intent.putExtra("membre", membre.toString());
startActivity(intent);}
catch(JSONException e){
Toast.makeText(MainActivity.this, "Erreur de connexion !",
Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(MainActivity.this, "Login ou mot de
passe non valide !",
Toast.LENGTH_SHORT).show();
}
}
}
}
A request is made on a local url http:///127.0.0.1/bio-relais/controleurMobil/json.php containing the code :
require_once '. /lib/autoloadMobil.php';
print(json_encode(MembreDAO::authentication($_POST['login'],
$_POST['mdp'])));
to pass the login and mdp of my Text edit in my method of the class MembreDao "authentification" containing the code :
public static function authentification($login, $mdp){
try{
$sql="select login, nomM ,prenomM
from MEMBRE
where login = :login
and mdp = :mdp ";
$requetePrepa = DBConnex::getInstance()->prepare($sql);
$mdp = md5($mdp);
$requetePrepa->bindParam("login", $login);
$requetePrepa->bindParam("mdp", $mdp);
$requetePrepa->execute();
$reponse = $requetePrepa->fetch(PDO::FETCH_ASSOC);
}catch(Exception $e){
$reponse = "";
}
return $reponse;
}
The expected result is a working authentification allowing the user present in the database to access to the interface of the MainProducteurActivity.
Thank you very much in advance for your help.
Solution
I finally found the problem, i was trying to connect to my localhost personal computer url on the avd with :
.url("http://127.0.0.1/bio- relais/controleurMobil/json.php")
But for the avd the localhost url of my json.php file is :
.url("http://10.0.2.2/bio-relais/controleurMobil/json.php")
( Localhost for avd = 10.0.2.2 ) .
Answered By - Bastian Robert
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.