所以这是我在一个教程视频中看到的代码,在该视频中,该应用程序运行正常,甚至显示了成功登录Toast。但是,当我尝试复制它时,即使我在数据库中输入了正确的用户名和密码,它也对我不起作用。 (仅供参考,我尝试使用用户的LAN ip以及WIFI IP在XAMPP上进行尝试,并且还尝试在000webhost.com上托管php脚本,但仍然无法在任何一种方法中工作)
我的ANDROID代码:
package com.example.devarsh.googlemapsbustracker;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private static final int ERROR_DIALOG_REQUEST = 9001;
private EditText User_name, Pass_word;
private Button btnlogin;
private static String URL_LOGIN =
"http://192.168.50.127/Android/login.php";
//vars
EditText enroll;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnlogin = findViewById(R.id.button2);
User_name = findViewById(R.id.username);
Pass_word = findViewById(R.id.password);
btnlogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Login();
}
});
isServicesOK();
}
private void Login(){
String URL =
"imagebustracker.000webhostapp.com/Android/parul/login.php";
RequestQueue requestQueue = Volley.newRequestQueue(this);
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if(response.trim().equals("success")){
Toast.makeText(getApplicationContext(), "Login Successfull", Toast.LENGTH_LONG).show();
Intent intent = new Intent(getApplicationContext(), Selection_Activity.class);
startActivity(intent);
}else{
Toast.makeText(getApplicationContext(), "Login Unsuccessfull", Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), "Login Unsuccessfull", Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("UserName", User_name.getText().toString().trim());
params.put("PassWord", Pass_word.getText().toString().trim());
return params;
}
};
requestQueue.add(stringRequest);
}
public void isServicesOK() {
Log.d(TAG, "isServicesOK: checking google services version");
int available = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(this);
if(available == ConnectionResult.SUCCESS){
//everything is fine and user can make map request
Log.d(TAG, "isServicesOK: Google Play Services is working");
}
else if(GoogleApiAvailability.getInstance().isUserResolvableError(available)){
//there is an error but we can fix it
Log.d(TAG, "isServicesOK: An error is there but we can fix it");
Dialog dialog = GoogleApiAvailability.getInstance().getErrorDialog(this, available, ERROR_DIALOG_REQUEST);
dialog.show();
}else{
Toast.makeText(this, "We cant make maps request", Toast.LENGTH_SHORT).show();
}
}
}
在Android代码中,我刚刚添加了一个内容,如果response.trim()。equals(“ success”)然后它将启动一个新活动,但它会继续举杯登录失败。
感谢所有为找到我的问题的答案而付出的努力。
非常感谢。
答案 0 :(得分:0)
首先,测试您的PHP脚本。因此,像这样
硬编码用户名和密码变量$username = "myUsername";
$password = "myPassword";
确保myUsername和myPassword是数据库中实际存在的数据。从那里,在您的Web浏览器中调用相同的URL,然后查看响应。是成功还是错误。
另一件事值得注意,当您拨打电话时,请在Android应用中阅读响应代码。响应代码将明确确定它是否正在与服务通信。但就您而言,我建议您打印出Volleyerror。
另外,向我提供您用于本教程的链接。有时对代码有双眼会很好。
正如您提到的,您会遇到“ Login Unsuccessful”吐司,这意味着它正在触发onErrorResponse函数。因此,将函数更改为以下内容:-
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), "Login Unsuccessfull", Toast.LENGTH_LONG).show();
Log.e(TAG, error); //or
System.println("VolleyError Start: " + error + " : End VolleyError");
}
请分享错误。如果您有任何问题,请告诉我。