当用户在Android设备上填充文本框时,我试图在phpMyAdmin数据库中插入新用户。为此,我编写PHP代码,我在POSTMAN上测试它的工作,但当我在Android真实设备上测试时,我得到了AuthFailureError!在POSTMAN中,我将授权设置为" No Auth"。 这是我在android studio中的代码:
public class Registration extends AppCompatActivity {
private EditText name,surname,email,password,adress;
private Button btnLog;
private RequestQueue requestQueue;
private static final String URL="http://192.168.*.*/Log/LogUser.php";
private StringRequest request;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registruj_se);
name=(EditText) findViewById(R.id.txtName);
surname=(EditText)findViewById(R.id.txtSurname);
email=(EditText)findViewById(R.id.txtEmail);
password=(EditText)findViewById(R.id.txtPassword);
adress=(EditText)findViewById(R.id.txtAdress);
btnLog=(Button)findViewById(R.id.btnLog);
requestQueue= Volley.newRequestQueue(this);
btnLog.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
request=new StringRequest(Request.Method.POST,URL,new Response.Listener<String>(){
@Override
public void onResponse(String response) {
try
{
JSONObject jsonObject=new JSONObject(response);
if(jsonObject.names().get(0).equals("success")){
Toast.makeText(getApplicationContext(),"success:"+jsonObject.getString("success"),Toast.LENGTH_SHORT).show();
startActivity(new Intent(getApplicationContext(),Welcome.class));
}else{ Toast.makeText(getApplicationContext(),"error:"+jsonObject.getString("error"),Toast.LENGTH_SHORT).show();}
}
catch (JSONException e){e.printStackTrace();}
}
},new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error) {
if (error instanceof NetworkError) {
Toast.makeText(getApplicationContext(),"Cannot connect to Internet...Please check your connection!",Toast.LENGTH_SHORT).show();
} else if (error instanceof ServerError) {
Toast.makeText(getApplicationContext(),"The server could not be found. Please try again after some time!!",Toast.LENGTH_SHORT).show();
} else if (error instanceof AuthFailureError) {
Toast.makeText(getApplicationContext(),"AuthFailureError",Toast.LENGTH_SHORT).show();
} else if (error instanceof ParseError) {
Toast.makeText(getApplicationContext(),"Parsing error! Please try again after some time!!",Toast.LENGTH_SHORT).show();
} else if (error instanceof NoConnectionError) {
Toast.makeText(getApplicationContext(),"NoConnectionError",Toast.LENGTH_SHORT).show();
} else if (error instanceof TimeoutError) {
Toast.makeText(getApplicationContext(),"Connection TimeOut! Please check your internet connection.",Toast.LENGTH_SHORT).show();
}
}
})
{
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String,String>hashMap=new HashMap<String, String>();
hashMap.put("name",name.getText().toString());
hashMap.put("surname",surname.getText().toString());
hashMap.put("email",email.getText().toString());
hashMap.put("password",password.getText().toString());
hashMap.put("adress",adress.getText().toString());
return hashMap;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
// do not add anything here
return headers;
}
@Override
public String getBodyContentType() {
return "application/json";
}
};
requestQueue.add(request);
}
});
我感谢任何帮助!
答案 0 :(得分:1)
试试这个:
在getBodyContentType()
方法中返回"application/x-www-form-urlencoded"
而不是"application/json"
<强>代码强>
@Override
public String getBodyContentType() {
return "application/x-www-form-urlencoded";
}