2017-07-08 72 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地址,並在此之後重新啓動所有服務。 –