2017-07-08 41 views
0

我想在用户在android设备上填充文本框时,在phpMyAdmin数据库中插入新用户。为此,我写的PHP代码,我测试它在POSTMAN和它的作品,但是当我在Android真实设备上测试时,我得到了AuthFailureError!在POSTMAN中,我将授权设置为“无验证”。 这是我在android工作室的代码:Volley-AuthFailureError

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); 
      } 
     }); 

我很感激任何帮助!

回答

1

试试这个:

getBodyContentType()方法返回"application/x-www-form-urlencoded"代替"application/json"

代码

@Override 
public String getBodyContentType() { 
     return "application/x-www-form-urlencoded"; 
} 
+0

这似乎是一个猜测......不知道为什么会影响一个AuthFailure –

+0

谢谢回复!我解决了我的问题。因此,任何在真实设备上测试应用程序的人都必须在您的移动设备的浏览器中键入您的计算机的IP地址,并且如果您看到类似“禁止访问/在此服务器上的权限”的操作,点击apache-> httpd.conf并找到全部拒绝,并替换为允许从您的手机的IP地址,并在此之后重新启动所有服务。 –