0

I want to save data acquired from Volley, But lambda used in VolleyRequest function(which gets json data from server) blocks it.

How should I change local variable that is in outside of lambda? Thanks in advance.

class ConDataforReturn( val title:String , val imgDataList: ArrayList<ConImgData>)

fun getConData(context: Context, idx : String):ConDataforReturn{
        val params = HashMap<String,String>()
        var cd = arrayListOf<ConImgData>()
        var title =""
        params.put("package_idx",idx)
        Log.e("idx size",idx.length.toString())
        VolleyRequest(context,params,"https://dccon.dcinside.com/index/package_detail") { response ->
            val answer = JSONObject(response)
            var json = answer.getJSONArray("detail")

            title = answer.getJSONObject("info").getString("title")
            Log.d("title",title)//Prints right data
            for (i in 0..(json.length() - 1)) {
                val v = json.getJSONObject(i)
                cd.add(ConImgData(v.getString("title"), v.getString("ext"), v.getString("path")))
            }

        }
        return ConDataforReturn(title,cd)//returns ConDataforReturn("",arrayListOf<ConImgData>())
    }
1
  • maybe this is of some use to you ? Commented Jan 28, 2020 at 5:33

1 Answer 1

2

Here the the code from were you are calling this method

getConData(this, "id") { condata ->

}

Now, your method look like this,

fun getConData(context: Context, idx : String, returnConData : (condata : ConDataforReturn) -> Unit){
        val params = HashMap<String,String>()
        var cd = arrayListOf<ConImgData>()
        var title =""
        params.put("package_idx",idx)
        Log.e("idx size",idx.length.toString())
        VolleyRequest(context,params,"https://dccon.dcinside.com/index/package_detail") { response ->
            val answer = JSONObject(response)
            var json = answer.getJSONArray("detail")

            title = answer.getJSONObject("info").getString("title")
            Log.d("title",title)//Prints right data
            for (i in 0..(json.length() - 1)) {
                val v = json.getJSONObject(i)
                cd.add(ConImgData(v.getString("title"), v.getString("ext"), v.getString("path")))
            }
            returnConData(ConDataforReturn(title,cd)) //returns ConDataforReturn("",arrayListOf<ConImgData>())
        }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Refer this official link [kotlinlang.org/docs/reference/lambdas.html ] for Kotlin Higher Order function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.