U
U
Usman Akhmedov2017-02-12 22:39:35
Java
Usman Akhmedov, 2017-02-12 22:39:35

How to implement JSON sending and receiving by server and client in Android using Spring?

How to implement JSON sending and receiving by server and client in Android using Spring?
Wrote such a controller on the server:

@RestController
@RequestMapping(value = "/students/")
public class AtheniumController {

    private final AtheniumService service;

    @Autowired
    public AtheniumController(AtheniumService service) {
        this.service = service;
    }

    @RequestMapping
            (value = "student/{studentSurname}, {studentName}, {studentPatronymic}, {studentNumber}", method = RequestMethod.POST)
    @ResponseBody
    public String checkLogin(@PathVariable("studentSurname") String studentSurname,
                             @PathVariable("studentNumber") String studentNumber,
                             @PathVariable("studentPatronymic") String studentPatronymic,
                             @PathVariable("studentName") String studentName) {

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

It receives the student's data from the client, and checks if one exists, if so, it saves it to the database. Then the client can get estimates and so on.
Can't send data from client:
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("studentSurname", pStudents[0].getStudentName());
      jo.put("studentSurname", pStudents[0].getStudentPatronymic());
      jo.put("studentSurname", 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;
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
aol-nnov, 2017-02-13
@aol-nnov

go to google, type "retrofit spring example" and the first link will be exactly what you are looking for :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question