我是Android的新手,为了准备上一次学校考试,我被要求在phpmyadmin(wampserver)上放置一个使用外部数据库的Android应用程序解决方案。
我的连接是在MainActivity文件中完成的,使用OkHttp3并使用对外部php文件的请求覆盖AsyncTask进行身份验证。
几天的工作后,连接仍然不起作用,并且我的测试即将开始,android应用程序的每次启动都处于调试模式,这是我尝试连接时的错误日志:
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'
我已经尝试通过AndroidManifest.xml添加Internet权限:
<uses-permission android:name="android.permission.INTERNET"/>
要添加okhttp3依赖项并将proxy选项置于自动检测状态。
MoreOver具有日志功能,我验证了我的EditText是否正常工作以及我的变量请求是否正确。
我认为错误出在我的MainActivity文件的第70-71行:
response = client.newCall(request).execute();
responseStr = response.body().toString();
我的MainActivity文件代码:
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();
}
}
}
}
对包含代码的本地URL http:///127.0.0.1/bio-relais/controleurMobil/json.php进行了请求:
require_once '. /lib/autoloadMobil.php';
print(json_encode(MembreDAO::authentication($_POST['login'],
$_POST['mdp'])));
在包含代码的类MembreDao“ authentification”的方法中传递Text编辑的登录名和mdp:
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;
}
预期结果是有效的身份验证,允许数据库中存在的用户访问MainProducteurActivity的界面。
非常感谢您的帮助。
答案 0 :(得分:0)
我终于找到了问题,我试图使用以下命令连接到avd上的localhost个人计算机URL:
.url("http://127.0.0.1/bio- relais/controleurMobil/json.php")
但是对于avd,我的json.php文件的localhost网址是:
.url("http://10.0.2.2/bio-relais/controleurMobil/json.php")
(avd的Localhost = 10.0.2.2)。