Hello, Khabrovsk citizens!
When you are developing network applications for the Android platform, you understand that one and the other application is similar to each other. With this thought, the majority of developers initialize another thought - “Why don’t I use any ready-made solutions, because obviously my task is not new?”. From this moment, the search begins for suitable libraries to solve the current issue. After some time - profit. Everything seemed to be good, because the library was found and it remained only to write an implementation of the solution to the current issue. And here the problems begin. They are associated with thread control, and indeed Java has never been famous for its compactness, all the "magic" should be implemented directly by the developer. Sometimes I find only positive aspects in this, because the logic of each project differs radically from each other. You constantly have to switch from stream to stream, which is not very convenient and, as described earlier, is cumbersome. The solution to this problem was implemented earlier, but not as compact as we would like. A library for working with HTTP comes to the rescue -
GridmiAPI . This is the most compact solution, which in turn allows you to operate with data in real time.
The working process
All library work begins with its initialization. Initialization is required once. Initialization refers to the following construction:
GridmiAPI.init("https://habr.com/API/", 8000, JSONObject.class);
- Request Processing Endpoint Address
- Server response timeout
- The class to which the server response should be cast
That's all! The library is ready for full use.
First server request
To complete the first request to the server, you need to write just a few lines of code and the data we need with us. Do not believe? See:
GridmiAPI.onRequest(this, new GridmiAPI.Request("profile/get"), new GridmiAPI.Handler.OUT() { @Override protected void onSuccess(GridmiAPI.Response response) { Log.d("TagGridmiAPI", "result = " + ((JSONObject) response.getData()).toString()); } @Override protected void onFailed(Exception exception) { Log.d("TagGridmiAPI", "exception = " + exception.getMessage()); } }).start();
Congratulations! This is your “Hello world!” Using this library. Let's look at two rewritten methods of the GridmiAPI.Handler.OUT class. To begin with, it should be written that these two methods are called in the main thread, so upon completion of the request, it makes no sense to access the activity to start the main thread. You just take JSON for example and change the text of the TextView. Everything is simple.
Send file to server
Very often you have to send files to the server. Headache again? No way. The GridmiAPI library makes sending a file very easy without a headache. Slightly more lines than in the example above, but still compact. Here's how to send a file:
@Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); if (data == null || data.getData() == null) return; try {
Conclusion
We examined the most popular tasks, in more detail you can familiarize yourself on the page of this library on GitHub -
GridmiAPI . It’s comfortable to work with this library. Estimate yourself. Thank you for your interest in this article!
References
GridmiAPI Library
Documentation / ReadMe.md