interface ApiInterface {
@Multipart
@POST("/")
suspend fun getRendomFact(
@Part("secret_key") secretKey: String,
@Part("token") token: String,
@Part("action") action: String
): Response<HomeData>
}
URL here
object Util {
const val Base = "https://example.com/guruji_apis/v1/index.php/"
}
Retrofit instance here
object RetrofitInstance {
val api : ApiInterface by lazy {
Retrofit.Builder()
.baseUrl(Util.Base)
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(ApiInterface::class.java)
}
}
This code is in main activity and calls when we click on button
fun sendResponse() {
GlobalScope.launch(Dispatchers.IO) {
val response = try {
RetrofitInstance.api.getRendomFact(
secretKey = "asdfghjkl12345",
token = "eyJ0R86Yz6XA8u_4",
action = "get_home_screen")
} catch (e: HttpException) {
Log.d("arvind,ex", "sendResponse: ${e}")
return@launch
} catch (e: IOException) {
Log.d("arvind,ioex", "sendResponse: ${e}")
return@launch
}
if (response.isSuccessful && response.body() != null) {
withContext(Dispatchers.Main) {
Log.d("arvind", "sendResponse: ${response.body()}")
}
}
else{
Log.d("arvind", "sendResponse: ${response}")
}
}
}
}
In this above code i have provided not working in form data and i am using this API doesn’t have any end point because we are calling it directly by sending action which is different for different data,
and using latest version of android studio and dependencies also
New contributor
Arvind Gehlod is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.