-1

I want to use @Rest api from android annotations to post application/json data to server. How can I do it? I want something like POST request send json data java HttpUrlConnection but as simple as android annotations.

thanks.

Community
  • 1
  • 1
sinoohe
  • 40,334
  • 3
  • 19
  • 16

3 Answers3

2

You can user Retrofit Type-safe REST client for Android and Java.

Use annotations to describe the HTTP request:

  • URL parameter replacement and query parameter

support Object

  • conversion to request body (e.g., JSON, protocol buffers)
  • Multipart request body and file upload

See here a good tutorial Consuming-APIs-with-Retrofit

Anderson K
  • 5,445
  • 5
  • 35
  • 50
1

You can use Gson library for this. Here and here you can find useful tutorials.

Guillermo Merino
  • 3,197
  • 2
  • 17
  • 34
1

Actually i think this is well documented in the AA wiki.

@Rest(rootUrl = "http://company.com/ajax/services", converters = { GsonHttpMessageConverter.class })
public interface MyRestClient {

  @Post("/events")
  void addEvent(Event event);
}

public class Event {
  String description;

  // ...
}

You you can use MappingJacksonHttpMessageConverter instead of GsonHttpMessageConverter if you are using Jackson instead of GSON.

Disclaimer: i am an AndroidAnnotations developer.

WonderCsabo
  • 11,947
  • 13
  • 63
  • 105
  • Could you tell me why you cannot use post with a set of arguments in the same way as Retrofit or a GET request? Why does AA have the need for a Post arguments class? – A. Steenbergen Jan 23 '16 at 15:13
  • Post parameter annotation is added in the next release of AA. If you need new features, feel free to contribute, as all AA developers do in their free time. – WonderCsabo Jan 23 '16 at 18:29
  • I didn't want to come off as needy and if I did, I apologize. You do great work with AA! Thank you very much for your good work and your quick response. – A. Steenbergen Jan 23 '16 at 22:22