// FULL EXAMPLE FOR RETROFIT //add Retrofit dependencies to gradle file//file build.gradele
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'com.squareup.okhttp3:okhttp:5.0.0-alpha.10'
// create an interface for more flexibility// file MyAPI.ktinterfaceMyAPI(){// function calling to server// @Field("your_name_of_field") under this name we will receive our request on the server// Like if(isset($_POST("your_name_of_field"))){ do something } //PHP@FormUrlEncoded// required annotation@POST("server.php")// address of the page to which we send data and from which we receive a response
fun SendAndGetPost(@Field("name") userName:String?@Field("password") userPass:String?):Call<ResponsedData?>?// class ResponsedData.kt
companion object{private val client =OkHttpClient.Builder().build()
operator fun invoke():MyAPI{returnRetrofit.Builder()// https://google.com/folder/ (path example).baseUrl("https://yor_server_path/").addConverterFactory(GsonConverterFactory.create()).client(client).build().create(MyAPI::class.java)}}}// create a class to receive our response from the server// file ResponsedData.ktclassResponsedData{
val email:String?=null
val age:String?=null//variable names must match the names sent from the server !}// ActivityMain calls the function and get the dataclassActivityMain:AppCompatActivity(){
override fun onCreate(savedInstance:Bundle){super.onCreate(savedInstance)setContentView(R.layout.activity_main)
val name ="myName"
val password ="1234567"MyAPI().SendAndGetPost(name, password)?enqueue(
object:CalBack<ResponsedData?>{
override fun onResponse(
call:Call<ResponsedData?>,
response:Response<ResponsedData?>){if(!response.isSuccessful){Log.e("API-ERROR", response.code().toString())return}else{// val answer: ResponsedData
val answer = response.body()
email.name
age.password
// data for use }}
override fun onFalure(
call:Call<ResponsedData?>,
t:Throwable){Log.e("API-ERROR", t.printStackTrace())}})}}---------------------SERVERSIDE-------------------------------------------// this is how the file on the server looks like // the path to which we registered in the interface of our application// file server.php<?php
// Data Base includes// or any sources if(isset($_POST("name"))&&isset($_POST("password"))){
$js =array('email'=>"some data from DB",'age'=>"some data from DB");
echo json_encode($js);exit();}// all examples are taken from here// https://square.github.io/retrofit/
publicinterfaceMyApiEndpointInterface{// Request method and URL specified in the annotation// Callback for the parsed response is the last parameter@GET("users/{username}")voidgetUser(@Path("username")String username,Callback<User> cb);@GET("group/{id}/users")voidgroupList(@Path("id")int groupId,@Query("sort")String sort,Callback<List<User>> cb);@POST("users/new")voidcreateUser(@BodyUser user,Callback<User> cb);}
publicinterfaceMyApiEndpointInterface{// Request method and URL specified in the annotation@GET("users/{username}")Call<User>getUser(@Path("username")String username);@GET("group/{id}/users")Call<List<User>>groupList(@Path("id")int groupId,@Query("sort")String sort);@POST("users/new")Call<User>createUser(@BodyUser user);}