Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
3.8 kB
5
Indexable
Never
private fun okhttpcall(projectid :String){
//        Log.d(TAG, "onFailure: "+projectid)
        imgstring = ""
        iddstring= ""
        projectId=""
        planid=""
        projectfilepath = ""
         levelname = ""
        val pd = ProgressDialog(this@LoginActivity)
        pd.setMessage("loading")
        pd.setCancelable(false)
        pd.show()
        val okHttpClient = OkHttpClient()
        val mediaType = "application/json; charset=utf-8".toMediaType()
        val requestBody = "{\r\n\"projectId\":\"$projectid\"\r\n}".toRequestBody(mediaType)
        val request = Request.Builder()
            .method("POST", requestBody)
            .url("https://stagegeospatial.digital.lntecc.com/dg-360-api/api/v1/getProjectLevelSection")
            .build()
        okHttpClient.newCall(request).enqueue(object :okhttp3.Callback {
            override fun onFailure(call: okhttp3.Call, e: IOException) {
                Log.d(TAG, "onFailure: "+e.message)
                pd.dismiss()
            }
            override fun onResponse(call: okhttp3.Call, response: okhttp3.Response) {
                val json = response.body!!.string()
                Log.d(TAG, "onResponse: "+json)
                 pd.dismiss()
                try {

                    levels_List = ArrayList()
                    levelidslist = ArrayList()
                    levelimglist = ArrayList()
                    val Jobject = JSONObject(json)

                    if (Jobject.has("data")) {

                    val Jarray = Jobject.getJSONArray("data")
                    val planchk = Jobject.getString("is_indoor")
                        if (Jobject.has("projectFilePath")) {
                            projectfilepath = Jobject.getString("projectFilePath")
                        }

                    projectId = Jobject.getString("projectId")
                    if (planchk != null && planchk.length > 0) {
                        if (planchk.equals("true")) {
                            planid = "Indoor"
                        } else if (planchk.equals("false")) {
                            planid = "Outdoor"
                        }
                    }
                    for (i in 0 until Jarray.length()) {

                        val objectval = Jarray.getJSONObject(i)
                        val idd = objectval.getString("id")
                        val name = objectval.getString("name")
                        val file_path = objectval.getString("file_path")
                        levels_List = levels_List + name
                        levelidslist = levelidslist + idd
                        levelimglist = levelimglist + file_path
//                        val info = Data(file_path = file_path, id = idd.toInt(), name = name)
                    }
                        runOnUiThread {
                            if (levels_List != null) {
                                loadDataIntoSpinner(levels_List)

                            }
                        }

                }else{
                        if (Jobject.has("message")) {
                            runOnUiThread {
                                Toast.makeText(this@LoginActivity, ""+Jobject.getString("message"), Toast.LENGTH_SHORT).show()
                            }
                        }
                    }
                } catch (e: JSONException) {
                    runOnUiThread {
                        Log.d(TAG, "onResponse: "+e.message)
                        Toast.makeText(this@LoginActivity, ""+"Something went wrong,Please try again", Toast.LENGTH_SHORT).show()
                    }
                    e.printStackTrace()
                }
            }
        })
    }
Leave a Comment