Retrofit Calling the Magneto API using @FormUrlEncoded getting issue while sending the data to the magneto server

本小妞迷上赌 提交于 2021-02-11 12:52:22

问题


override fun submitSupportTicket(createSupportTicket: MutableLiveData, question: String, details: String, department: String, filePath: String) { var getSupporttUrl = "https://xxxxxxxx/V1/ecomapi/postNewSupportTicket" if (filePath.isEmpty()) { /* var getDepartmentInput = Constants.getDefaultParameterJasonInput(Constants.getUserId()) getDepartmentInput.put("questionText",question) getDepartmentInput.put("detailText",details) getDepartmentInput.put("departmentId",department)*/

       /* val requestMap: MutableMap<String, String> = HashMap()
        requestMap["questionText"] = String(question.toByteArray(), charset("UTF-8"))
        requestMap["detailText"] = String(details.toByteArray(), charset("UTF-8"))
        requestMap["departmentId"] = String(department.toByteArray(), charset("UTF-8"))
        requestMap["customerId"] = String(Constants.getUserId().toByteArray(), charset("UTF-8"))

        val dataSupport = CreateSupportTicketModel(String(question.toByteArray(), charset("UTF-8")),
                String(details.toByteArray(), charset("UTF-8")),
                String(department.toByteArray(), charset("UTF-8")),
                String(Constants.getUserId().toByteArray(), charset("UTF-8")))

        val request = Gson().toJson(dataSupport)

        val params: MutableMap<String, String> = HashMap()
        params["data"] = request*/

        val bodyQuestion: RequestBody = RequestBody.create("text/plain".toMediaTypeOrNull(), question)
        val bodyDetails: RequestBody = RequestBody.create("text/plain".toMediaTypeOrNull(), details)
        val bodyDepartment: RequestBody = RequestBody.create("text/plain".toMediaTypeOrNull(), department)
        val bodyUserId: RequestBody = RequestBody.create("text/plain".toMediaTypeOrNull(), Constants.getUserId())


        mView.getAPIComponent().retroService.getBaseUrl()
                .createSupportTicketWithoutImage(Constants.getHeaderTokenForApisWithCType(),
                        getSupporttUrl, bodyQuestion,bodyDetails,bodyDepartment,bodyUserId
                )
                .enqueue(object : CustomRetroCallbacks<Any>() {

                    override fun onSuccess(resonse: Any?) {
                        createSupportTicket.value = resonse
                    }

                    override fun onError(response: Any?) {

// createSupportTicket.value = response?.message }

                    override fun onFailure(generalErrorMsg: String, systemErrorMsg: String) {
                        mView.getDepartmentListFailed(generalErrorMsg)
                    }
                })

    } else {
        var imagePart: MultipartBody.Part? = null
        if (!Utility.isBlankString(filePath)) {
            val imageFile = File(filePath)
            val inputStream = mView.context(true).contentResolver.openInputStream(Uri.fromFile(imageFile))
            imagePart = MultipartBody.Part.createFormData("attachment[]", imageFile.name, imageFile.toImagePartBody())
        }

        val requestBodyQuestion: RequestBody = question.toRequestBody("*/*".toMediaTypeOrNull())
        val requestBodyDepartment: RequestBody = department.toRequestBody("*/*".toMediaTypeOrNull())
        val requestBodyDetails: RequestBody = details.toRequestBody("*/*".toMediaTypeOrNull())
        val requestBodyUserId: RequestBody = Constants.getUserId().toRequestBody("*/*".toMediaTypeOrNull())


        mView.getAPIComponent().retroService.getBaseUrl()
                .createSupportTicket(Constants.getHeaderTokenForApisWithCType(), getSupporttUrl,
                        imagePart,
                        requestBodyQuestion,
                        requestBodyDetails,
                        requestBodyDepartment,
                        requestBodyUserId)
                .enqueue(object : CustomRetroCallbacks<Any>() {

                    override fun onSuccess(resonse: Any?) {
                        createSupportTicket.value = resonse
                        createSupportTicket.value = false
                    }

                    override fun onError(response: Any?) {
                        createSupportTicket.value = false

// createSupportTicket.value = response?.message }

                    override fun onFailure(generalErrorMsg: String, systemErrorMsg: String) {
                        createSupportTicket.value = false
                        mView.getDepartmentListFailed(generalErrorMsg)
                    }
                })
    }
}

API INTERFACE

@POST
@Multipart
fun createSupportTicketWithoutImage(@HeaderMap headers: Map<String, String>,
                                    @Url url: String?,
                                    @Part("questionText ") questionText:RequestBody,
                                    @Part("detailText ") detailText:RequestBody,
                                    @Part("departmentId ") departmentId:RequestBody,
                                    @Part("customerId ") customerId:RequestBody ): Call<Any>

来源:https://stackoverflow.com/questions/65125422/retrofit-calling-the-magneto-api-using-formurlencoded-getting-issue-while-sendi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!