lundi 27 mai 2019

Volley return null in kotlin

I have this some codes of Volley which retrieve some data from web api that I already use before and it worked. But now I want to use it, it returning null. I already test the web api on postman, it's work fine, still returning the same json as before. I already look up other way of the volley code. But it doesn't works well after I run it multiple times (it returns nothing, I already displayed the log and nothing happened).

this is my method that I called on my onCreate

private fun loadData() {
        val stringRequest = StringRequest(Request.Method.GET,
           EndPoints.URL_GET_USER,
            Response.Listener<String> { s ->
                try {
                    val obj = JSONObject(s)
                    Log.d("S", s.toString())
                    if (!obj.equals(" ")) {
                        val array = obj.getJSONArray("hak_akses")
                        for (i in 0..array.length() - 1) {
                            val objectArtist = array.getJSONObject(i)
                            objectArtist.getString("idpegawai")
                            Log.e("test", objectArtist.getString("idpegawai"))
                        }
                    } else {
                        Toast.makeText(getApplicationContext(), obj.getString("message"), Toast.LENGTH_LONG).show()
                    }
                } catch (e: JSONException) {
                    e.printStackTrace()
                }
            },
            Response.ErrorListener { volleyError ->
                Toast.makeText(applicationContext, volleyError.message, Toast.LENGTH_LONG).show()
                Log.e("ERROR", volleyError.message.toString())
            })
        Log.e("REQ", stringRequest.toString())
        val requestQueue = Volley.newRequestQueue(this)
        requestQueue.add<String>(stringRequest)
    }

this is the Volley Singleton

class VolleySingleton : Application() {
    override fun onCreate() {
        super.onCreate()
        instance = this
    }

    private val requestQueue: RequestQueue? = null
        get() {
            if (field == null) {
                return Volley.newRequestQueue(applicationContext)
            }
            return field
        }

    fun <T> addToRequestQueue(request: Request<T>) {
        request.tag = TAG
        requestQueue?.add(request)
    }

    companion object {
        private val TAG = VolleySingleton::class.java.simpleName
        @get:Synchronized var instance: VolleySingleton? = null
            private set
    }
}

this is my manifest

<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:name=".VolleySingleton"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        >

this is the json

{
    "hak_akses": [
        {
            "idpegawai": "3313"
        },
        {
            "idpegawai": "5181"
        },
        {
            "idpegawai": "3325"
        }
    ]
}

this is the log result

E/REQ: [ ] http://10.20.2.14//simserv/index.php?fun=tes 0xe2d95e40 NORMAL null




Aucun commentaire:

Enregistrer un commentaire