API call fixed

This commit is contained in:
mohamedkaif356
2023-07-19 16:49:33 +05:30
parent 2bd9f79795
commit 229f79fe24
3 changed files with 30 additions and 21 deletions

View File

@@ -17,7 +17,7 @@ import retrofit2.http.Part
interface MolbioApi {
@Headers("Content-Type: application/json")
@POST("devices/login")
fun login(@Body jsonObject: JsonObject): MolbioLogin
suspend fun login(@Body jsonObject: JsonObject): MolbioLogin
@Multipart
@@ -26,6 +26,6 @@ interface MolbioApi {
@Multipart
@PUT("results/upload")
fun uploadNewPdf(@Header("Authorization") auth:String,@Part("") count:RequestBody,@Part("") userId:RequestBody,@Part file_csv: MultipartBody.Part,@Part file_log:MultipartBody.Part,@Part("DeviceId") name:RequestBody): UploadResponse
suspend fun uploadNewPdf(@Header("Authorization") auth:String,@Part("") count:RequestBody,@Part("") userId:RequestBody,@Part file_csv: MultipartBody.Part,@Part file_log:MultipartBody.Part,@Part("DeviceId") name:RequestBody): UploadResponse
}

View File

@@ -41,7 +41,8 @@ class HomeFragment : Fragment() {
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
): View {
_binding = FragmentHomeBinding.inflate(inflater, container, false)
sharedPreference = requireContext().getSharedPreferences("PREFERENCE_NAME", Context.MODE_PRIVATE)
sharedPreference =
requireContext().getSharedPreferences("PREFERENCE_NAME", Context.MODE_PRIVATE)
DataHolder.selectedTest = null
return binding.root
@@ -83,35 +84,45 @@ class HomeFragment : Fragment() {
// jobScheduler.schedule(jobInfo)
}
private fun login()
{
val jsonObject= JsonObject();
jsonObject.addProperty("Username","hposdevice");
jsonObject.addProperty("Password","sickle");
viewModel.login(jsonObject)
viewModel.loginResponse.observe(viewLifecycleOwner) {response ->
when(response) {
override fun onStart() {
super.onStart()
val jsonObject = JsonObject()
jsonObject.addProperty("Username", "hposdevice")
jsonObject.addProperty("Password", "sickle")
viewModel.loginResponse.observe(viewLifecycleOwner) { response ->
when (response) {
is Result.Success -> {
val editor = sharedPreference.edit();
val editor = sharedPreference.edit()
editor.putString(Constants.TOKEN, response.data.token)
editor.apply()
Log.d("login_api","called");
// showUploadDialog()
Log.d("login_api", "called")
// showUploadDialog()
}
is Result.Error -> {
if (loginCount < 3) {
viewModel.login(jsonObject)
loginCount++
}
Log.d("API Error", response.exception.message.toString())
}
is Result.Loading -> {
is Result.Loading -> {
Toast.makeText(requireContext(), "Loading", Toast.LENGTH_SHORT).show()
}
else -> {}
}
}
}
private fun login() {
val jsonObject = JsonObject()
jsonObject.addProperty("Username", "hposdevice")
jsonObject.addProperty("Password", "sickle")
viewModel.login(jsonObject)
}
private fun setUserId() {
binding.btnSubmit.setOnClickListener {
val userId = binding.userId.text.toString()
@@ -143,6 +154,7 @@ class HomeFragment : Fragment() {
apply()
}
}
private fun logoutUser(context: Context) {
val builder = AlertDialog.Builder(context)
builder.setTitle("Log Out")
@@ -165,7 +177,6 @@ class HomeFragment : Fragment() {
}
private fun getData(search: String?, field: String) {
val capitalizedSearch = search?.replaceFirstChar {
if (search.lowercase()
@@ -248,7 +259,7 @@ class HomeFragment : Fragment() {
}
private fun uploadLocalDBData(dialog: DialogInterface) {
val token = sharedPreference.getString(Constants.TOKEN,"")
val token = sharedPreference.getString(Constants.TOKEN, "")
viewModel.allUserData.observe(viewLifecycleOwner) { userDataList ->
if (userDataList.isEmpty()) {
dialog.dismiss()
@@ -258,9 +269,7 @@ class HomeFragment : Fragment() {
}
dialog.dismiss()
Toast.makeText(
requireContext(),
R.string.upload_success_message,
Toast.LENGTH_SHORT
requireContext(), R.string.upload_success_message, Toast.LENGTH_SHORT
).show()
}
}

View File

@@ -62,7 +62,7 @@ class TestRightResults : Fragment() {
binding.progressBar.visibility = View.VISIBLE
}
else -> {
Toast.makeText(requireContext(), "Test upload failed, please try again", Toast.LENGTH_SHORT).show()
Toast.makeText(requireContext(), "Test upload failed, please try again $it", Toast.LENGTH_SHORT).show()
}
}
}