api

mail@pastecode.io avatarunknown
java
a month ago
7.0 kB
1
Indexable
Never
 fun sendImage2(
        btnid: Int,
        compass: String,
        xaxis: String,
        yaxis: String,
        imgfile: String) {

        GlobalScope.launch(Dispatchers.IO) {
// ...
            runOnUiThread {
                val client = OkHttpClient.Builder()
                    .connectTimeout(300, TimeUnit.MINUTES)
                    .readTimeout(300, TimeUnit.MINUTES)
                    .writeTimeout(300, TimeUnit.MINUTES)
                    .build()
// ...
                val imgpath = File(imgfile)
                val uniquwname = generateUniqueFileName("jpg")
// Create the image RequestBody
                runOnUiThread {
                    btnsubmit.setBackgroundResource(R.drawable.roundedshapehide)
                    btnsubmit.isEnabled = false

                }

                val info = database.userDao().getallprojectdata()
                val levelid = info.get(0).idd
                val url = "https://stagegeospatial.digital.lntecc.com/dg-360-api/api/v1/addModule"

                var progressDialog = ProgressDialog(this@MainActivity)
                val sdf = SimpleDateFormat("yyyy-MM-dd")
//        val currentDate = sdf.format(Date())
                var projectid = info.get(0).projectId
                val datcur = info.get(0).createdDate
                var compsval = "0.0"
                if (compass.length > 0) {
                    compsval = compass
                } else {
                    compsval = "0.0"
                }

                val multipartBody: MultipartBody = MultipartBody.Builder()
                    .setType(FORM)
                    .addFormDataPart(
                        "file",
                        uniquwname,
                        imgpath.asRequestBody("image/jpeg".toMediaTypeOrNull())
                    )
                    .addFormDataPart("createdAt", datcur)
                    .addFormDataPart("projectId", projectid)
                    .addFormDataPart("levelSectionId", levelid)
                    .addFormDataPart("planId", btnid.toString())
                    .addFormDataPart("latitude", xaxis)
                    .addFormDataPart("longitude", yaxis)
                    .addFormDataPart("imageDirection", compsval)
                    .build()
// ...
// Build the MultipartBody using the custom ProgressRequestBody
                val progressRequestBody = ProgressRequestBody(multipartBody) { progress ->
                    runOnUiThread {
                        progressDialog.setMessage("Pleas wait uploading images... ")
                        progressDialog.setCancelable(false)
                        progressDialog.show()
                    }
                }

// Build the request with the custom ProgressRequestBody
                val request = Request.Builder()
                    .url(url)
                    .method("POST", progressRequestBody)
                    .build()

                client.newCall(request).enqueue(object : Callback {
                    override fun onFailure(call: Call, e: IOException) {
                        runOnUiThread {
                            progressDialog.dismiss()
                            btnsubmit.setBackgroundResource(R.drawable.roundedshape)
                            btnsubmit.isEnabled = true
                            Toast.makeText(this@MainActivity, e.toString(), Toast.LENGTH_SHORT)
                                .show()
                        }
// Handle failure
                    }
                    override fun onResponse(call: Call, response: Response) {
                        runOnUiThread {
                            try {
                                btnsubmit.setBackgroundResource(R.drawable.roundedshape)
                                btnsubmit.isEnabled = true
                                val json = response.body!!.string()
//                            onComplete(response.isSuccessful, json)
                                Log.d(ContentValues.TAG, "onResp ulpoaded image-----" + json)
//                            pd.dismiss()
                                val jsonObject = JSONObject(json)
                                val msg = jsonObject.get("message")
                                val sts = jsonObject.get("status")
                                val mid = jsonObject.get("id")

                                if (sts == false) {
                                    Toast.makeText(this@MainActivity, msg.toString(), Toast.LENGTH_SHORT).show()
                                    progressDialog.dismiss()
                                    btnsubmit.setBackgroundResource(R.drawable.roundedshape)
                                    btnsubmit.isEnabled = true
//                            Log.d(ContentValues.TAG, "onResp ulpoaded image" + msg+sts)

                                }
                                else if (sts == true) {

                                    database.userDao().updateMid(btnid, mid.toString())
                                    btnsubmit.isEnabled = true
                                    if (database != null) {
                                        val listw = database.userDao().getallPoints()

                                        if (listw != null && listw.size > 0) {
                                            for (i in 0 until listw.size) {
                                                if (listw.get(i).id.equals(btnid)) {
//                                val byteArray = ByteArray(0)
                                                    database.userDao().changeimageSts(btnid, true)
                                                }
                                            }
                                        }
                                    }
                                    progressDialog.dismiss()
                                    Toast.makeText(
                                        this@MainActivity,
                                        "Image uploaded successfully",
                                        Toast.LENGTH_SHORT
                                    ).show()
                                    if (inorout == 0) {
                                        retrive_Indoor_Points()
                                    } else if (inorout == 1) {
                                        addmarkers()
                                    }
                                }

                            } catch (e: Exception) {
                                progressDialog.dismiss()
                                btnsubmit.setBackgroundResource(R.drawable.roundedshape)
                                btnsubmit.isEnabled = true

                            }
                        }
                    }
                })
            }
        }
       
    }