Perhaps the silly question will be asked now, because I am a newcomer in Java programming and in the development of Android as a whole, so I want to apologize in advance.
I need to get data from the API using Retrofit 2.0. I watched all sorts of trailings on the Internet, moreover, I even got the list of GitHub repositories. But I do not understand how to receive is not a list, but something one, concrete. For example, at the moment I need to send an e-mail and password settings method / Login to get the authorization token. But, as already mentioned above, I can’t figure out how 🙁
If someone could somehow help me somehow, I would be very grateful. I do not ask for me the code for me, please help figure it out in everything.
Answer 1, Authority 100%
Successful query results from Postman Copy and insert in http://www.jsonschema2pojo.org/, the received files, if necessary, adjust and insert the Android Studio to the project to the directory (Package) with the name model – the name is optional, just just traditionally settled. After that, you write two modules: factory and service, and put them in the project in the Network directory.
factory code:
// Import the necessary classes
Import retrofit2.retrofit;
Import retrofit2.converter.gson.gsonconverterfactory;
// We declare the factory - only static fields and methods
Public Class Apifactory {
Private Static Final String Root_URL = "https: // your_api_address";
Static Retrofit BuildRetrofit () {
Return New Retrofit.Builder ()
.BaseURL (root_url)
.addconverterfactory (gsonconverterfactory.create ())
.build ();
}
Public Static Apiservice GetService () {
RETURN BUILDRETROFIT (). CREATE (Apiservice.class);
}
}
Service code (taking into account the POST request and input parameters Login and password):
// import the model you got in json-pojo
// In this case, you must call your ACCESSTOKEN model
Import com.your_brand.your_app_name.model.accesstoken;
Import retrofit2.call;
Import retrofit2.http.post;
Import retrofit2.http.query;
Import retrofit2.http.headers;
Public Interface Apiservice {
@Headers ("Content-Type: Application / JSON")
@Post ("Auth / Login") // The path of your service to authorization
Call & Lt; Accesstoken & GT; GetToken (
@Query ("Login") String Login,
@Query ("Password") STRING PASSWORD);
}
In the main part of your application to receive a token in response to a login and password, you are prescribed approximately this (taking into account how variables are named, classes, etc.):
Private void GetToken () {
Call & Lt; Accesstoken & GT; Call = Apifactory.GetService (). GetToken (email, password);
call.enqueue (New Callback & Lt; Accesstoken & GT; () {
@Override
Public Void Onresponse (Call & Lt; Accesstoken & GT; Call, Response & LT; Accesstoken & GT; Response) {
If (response.issuccessful ()) {
// here you just keep the token for further use
MainActivity.Token = Response.Body (). GetData (). GetToken ();
MainActivity.Tokenacquired = True;
Log.d ("MyLogs", Token);
GetTasks ();
} else {
MainActivity.Token = "";
MainActivity.Tokenacquired = false;
Log.d ("MyLogs", errorutils.errormessage (Response));
}
}
@Override
Public Void Onfailure (Call & Lt; Accesstoken & GT; Call, Throwable T) {}
});
}
Errorutils class (optional, only for error boarding):
package com.your_brand.your_app_name.network;
Import java.io.ioException;
Import java.lang.annotation.annotation;
Import java.util.locale;
Import okhttp3.responsebody;
Import retrofit2.converter;
Import retrofit2.response;
Public Class Errorutils {
Private Static Final String Error_Message = "HTTP code:% d \ N error:% s \ Nc error:% d";
Private Static Apierror Parseerror (Response & LT;? & GT; Response) {
Converter & LT; Responsebody, Apierror & GT; converter =.
Apifactory.buildretrofit ()
.responsebodyconverter (apierror.class, new annotation [0]);
Apierror Error;
Try {
error = converter.converty (response.errorbody ());
} Catch (IoException E) {
Return New Apierror ();
}
RETURN ERROR;
}
Public Static String ErrorMessage (Response & Lt;? & GT; Response) {
Try {
APIERROR ERROR = PARSEERROR (RESPONSE);
Return String.Format (Locale.getDefault (), Error_Message, Response.code (), Error.Message (), error.status ());
} Catch (com.google.gson.jsonsyntaxexception e) {
Return String.Format (Locale.getDefault (), Error_Message, Response.code (), "NULL", 0);
}
}
}
APIERROR class
package com.your_brand.your_app_name.network;
Import com.google.gson.annotations.serializedName;
Public Class Apierror {
@SerializedName ("httpstatus")
Private Int Statuscode;
@SerializedName ("ErrorMessage")
Private String Message;
Public Apierror () {
}
Public int status () {
Return Statuscode;
}
Public String Message () {
Return Message;
}
}
Well, here’s information for reflection. com.your_brand.your_app_name is the name of your project specified in the Java module headers. While disassemble and good night. If any questions arise – ask, but I will answer tomorrow. Sorry in advance if some typos will be in the code. Successful work!