I want to make a server on which student data will be stored (grades, record number, first name, last name, etc.). For implementation I use Spring . I also make a client for Android , in which the user enters data, they are sent to the server, they are checked there.

How to send data to the server? Code written by me returns nothing.

Server:

@RestController @RequestMapping(value = "/students") public class AtheniumController { private final AtheniumServiceImpl service; @Autowired public AtheniumController(AtheniumServiceImpl service) { this.service = service; } @RequestMapping("/student/{studentSurname}/{studentName}/{studentPatronymic}/{studentNumber}") @ResponseBody public String checkLogin(@PathVariable(value = "studentSurname") String studentSurname, @PathVariable(value = "studentName") String studentName, @PathVariable(value = "studentPatronymic") String studentPatronymic, @PathVariable(value = "studentNumber") String studentNumber) { Student lvStudent = new Student(studentSurname, studentName, studentPatronymic, Long.valueOf(studentNumber)); if (service.checkLogin(lvStudent)) { return "OK"; } else { return "Проверьте введенные данные! (Также возможна проблема в серверах ДГУ)"; } } @RequestMapping(value = "marks", method = RequestMethod.GET) @ResponseBody public List<Mark> getMarks() { return service.getMarks(); } @RequestMapping(value = "marks/{id}", method = RequestMethod.DELETE) @ResponseBody public void delete(@PathVariable long id) { service.remove(id); } } 

Android:

 public class RequestRegister extends AsyncTask<Student, Void, String> { BufferedOutputStream bos; @Override protected String doInBackground(Student... pStudents) { try { URL url = null; try { url = new URL(Constants.POST_STUDENT); } catch (MalformedURLException pE) { pE.printStackTrace(); } HttpURLConnection urlConnection = null; try { assert url != null; urlConnection = (HttpURLConnection) url.openConnection(); } catch (IOException pE) { pE.printStackTrace(); } assert urlConnection != null; urlConnection.setRequestProperty("Content-Type", "application/json"); urlConnection.setRequestMethod("POST"); urlConnection.setDoInput(true); urlConnection.setDoOutput(true); urlConnection.connect(); JSONObject jo = new JSONObject(); jo.put("studentSurname", pStudents[0].getStudentSurname()); jo.put("studentName", pStudents[0].getStudentName()); jo.put("studentPatronymic", pStudents[0].getStudentPatronymic()); jo.put("studentNumber", pStudents[0].getStudentNumber()); bos = new BufferedOutputStream(urlConnection.getOutputStream()); bos.write(jo.toString().getBytes()); String result = urlConnection.getResponseMessage(); } catch (JSONException | IOException e) { e.printStackTrace(); } finally { try { bos.flush(); //очищает поток output-a bos.close(); } catch (IOException e) { e.printStackTrace(); } //urlConnection.disconnect(); } //return null; return null; } 
  • what means nothing returns? An empty response comes in, an http error arrives, or on the server exception? Ps on what urla knock on the server? - Artem Konovalov
  • If you try using a browser, typing a link - an error. If through the application - it just does not load. The interface slows down, as if I wrote the code in the main thread and nothing happens. - Usman Ahmedov
  • what url to connect using? - Artem Konovalov
  • Local Ip-adress company running Tomcat with this server - Akhmedov Usman
  • write url with all parameters - Artem Konovalov

1 answer 1

The problem is that you put a method on @PathVariable , and send an application/json request. To get Data from RequestBody you need to annotate the attributes of the controller method via @RequestParam or provide a POJO object that will be automatically filled with data from the request.

It will look like this:

 @RequestMapping(value = "/student", method = RequestMethod.POST) @ResponseBody public String checkLogin(@RequestParam(value = "studentSurname") String studentSurname, @RequestParam(value = "studentName") String studentName, @RequestParam(value = "studentPatronymic") String studentPatronymic, @RequestParam(value = "studentNumber") String studentNumber) { Student lvStudent = new Student(studentSurname, studentName, studentPatronymic, Long.valueOf(studentNumber)); if (service.checkLogin(lvStudent)) { return "OK"; } else { return "Проверьте введенные данные! (Также возможна проблема в серверах ДГУ)"; } } 

But it is advisable to use POJO and return ResponseEntity with HttpStatus .

 @RequestMapping(value = "/student", method = RequestMethod.POST) @ResponseBody public ResponseEntity<?> checkLogin(Student student) { if (service.checkLogin(student)) { return ResponseEntity.ok(); } else { return ResponseEntity.badRequest().body("Проверьте введенные данные! (Также возможна проблема в серверах ДГУ)"); } } 

At the apogee, it is generally possible to handle errors in services and logic not in the controller, but in @ControllerAdvice and @ExceptionHandler , but this is a completely different story.