0% found this document useful (0 votes)
39 views3 pages

Apiconnecton Retrofit KT

The document describes how to make an API call to retrieve data from a server using Retrofit in Android. It includes creating an API interface, service manager class, making the API call from an activity, and defines the data model classes.

Uploaded by

jv1256671
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views3 pages

Apiconnecton Retrofit KT

The document describes how to make an API call to retrieve data from a server using Retrofit in Android. It includes creating an API interface, service manager class, making the API call from an activity, and defines the data model classes.

Uploaded by

jv1256671
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Manifest-------------

---------------------
<uses-permission android:name="android.permission.INTERNET" />

android:usesCleartextTraffic="true"

gradle---

implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'

create class for connection----


--------------------------------
object ApiClient {
private const val BASE_URL = "http://103.122.38.34:5888/MilkProcurement.asmx/"

val retrofit: Retrofit = Retrofit.Builder()


.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
}

interface for define service name-----


-------------------------------------
interface ApiService {
@POST("CheckLoginDetails")
@FormUrlEncoded
fun getSomeData(
@Field("Key") op: String,
@Field("APPName") APPName: String,
@Field("APPVersion") APPVersion: String,
@Field("APPUserCode") APPUserCode: String,
@Field("APPUserPwd") APPUserPwd: String,
@Field("UserID") UserID: String,
@Field("Pwd") Pwd: String
): retrofit2.Call<YourDataModel>

Service Mnager class----


-------------------------
class ApiServiceManager {
private val apiService: ApiService =
ApiClient.retrofit.create(ApiService::class.java)
fun fetchDataFromApi( op: String,
APPName: String,
APPVersion: String,
APPUserCode: String,
APPUserPwd: String,
UserID: String,
Pwd: String
): Call<YourDataModel> {
return apiService.getSomeData(op, APPName, APPVersion, APPUserCode,
APPUserPwd,UserID,Pwd)
}
}

MainActivity---------
------------------------
class MainActivity : AppCompatActivity() {
val op = "Tecxpert@MP#123\$456%789^"
val appName = "com.tecxpert.saras_pro"
val appVersion = "33"
val appUserCode = "arjun"
val appUserPwd = "a"
val UserID = "arjun"
val Pwd = "a"
var yourDataMode: ArrayList<lovingItem> = ArrayList()
private val apiServiceManager = ApiServiceManager()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
fetchData()
}
private fun fetchData() {
val call: Call<YourDataModel> = apiServiceManager.fetchDataFromApi(op,
appName, appVersion, appUserCode, appUserPwd,UserID,Pwd)

call.enqueue(object : Callback<YourDataModel> {
override fun onResponse(call: Call<YourDataModel>, response:
Response<YourDataModel>) {
if (response.isSuccessful) {
val dataItem = response.body()
yourDataMode =java.util.ArrayList<lovingItem>()
yourDataMode = dataItem?.loving as
java.util.ArrayList<lovingItem>
if (yourDataMode != null) {
Toast.makeText(
applicationContext, "data save succesfully",
Toast.LENGTH_LONG
).show()
}
// Handle the data here
} else {
// Handle the error
}
}

override fun onFailure(call: Call<YourDataModel>, t: Throwable) {


// Handle network errors
}
})
}
}

Model class----
-------------------
data class YourDataModel (
@field:SerializedName("LoginData")
val loving: List<lovingItem?>? = null
)
data class lovingItem(

@field:SerializedName("MCC_Code")
val MCC_Code: String? = null,

@field:SerializedName("MCC_NAME")
val MCC_NAME: String? = null,

@field:SerializedName("Mcc_Code_VLC_Uploader")
val Mcc_Code_VLC_Uploader: String? = null,

@field:SerializedName("User_APP_Type")
val User_APP_Type: String? = null,

@field:SerializedName("Vendor_Code")
val Vendor_Code: String? = null,

@field:SerializedName("VLC_Code_VLC_Uploader")
val VLC_Code_VLC_Uploader: String? = null,

@field:SerializedName("Vendor_Name")
val Vendor_Name: String? = null,

@field:SerializedName("Current_UserCode")
val Current_UserCode: String? = null,

@field:SerializedName("Current_UserName")
val Current_UserName: String? = null,

@field:SerializedName("MP_Code")
val MP_Code: String? = null,

@field:SerializedName("MP_Code_VLC_Uploader")
val MP_Code_VLC_Uploader: String? = null,

@field:SerializedName("MP_Name")
val MP_Name: String? = null,

@field:SerializedName("Result")
val Result: String? = null,

You might also like