2017-06-18 123 views
0

我正在嘗試使用Volley從我的android手機發送到Spring上編寫的HTTP服務器的GET請求。使用Google Volley發送請求參數

我做了延伸Request並覆蓋其getParams()梅託德自定義請求:

@Override 
protected Map<String, String> getParams() throws AuthFailureError { 
     Map<String, String> parameters = new HashMap<>(); 
     parameters.put("email", "[email protected]"); 
     parameters.put("password", "xxxx");  
     return parameters; 
} 

:我將我在這樣的請求:

GsonRequest = new GsonRequest<String>(Request.Method.GET, "http://192.168.43.15:10000/login", 
       new Response.Listener<String>() { 
        @Override 
        public void onResponse(String response) { 
         .... 
        } 
       }, 
       new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 
         ...... 
        } 
     }); 
Log.i(TAG, new String(request.getBody())); 
requestQueue.add(request); 

日誌顯示請求有這個參數:

email = xxxx%40xxx.com & password = xxxx &

但有一個錯誤:

Unexpected response code 400 for http://192.168.43.15:10000/login

它看起來像請求參數不發送。


這是我的服務器代碼:

@RequestMapping(path = "/login", method = RequestMethod.GET) 
public ResponseEntity<String> login(@RequestParam(name = "email") String email, 
            @RequestParam(name = "password") String password) { 
    User user = repository.findByEmailAndPassword(email, password); 
    if (user != null) { 
     return new ResponseEntity("Found", HttpStatus.OK); 
    } 

    return new ResponseEntity<>("Not found", HttpStatus.OK); 
} 

我覺得這事不對我的Android設備上的代碼,因爲當我提出從瀏覽器的一切請求確定。

有人能告訴我問題在哪裏嗎?

+0

願你可以使用** ** StringRequest而不是** ** JSONRequest因爲你正在返回一個字符串響應。 – Frank

+0

它看起來像你想發送一些數據到服務器,但你使用GET方法,嘗試使用POST方法,當數據庫沒有收到強制參數時出現錯誤。 –

回答

相關問題