Ошибка при анализе ответа залпа в kotlin - PullRequest
0 голосов
/ 15 марта 2020

Я новичок в Kotlin программировании, я пытаюсь получить JSONObject из моего строкового ответа, но я не могу этого сделать.

Вот мой код:

fun getPosts() {

        val url = BASE_URL + GET_POST + "?UserID=${globalUserProfile?.userID}&PostOwnerID=${globalUserProfile?.userID}"

        val queue = Volley.newRequestQueue(activity)

        val stringRequest = object: StringRequest(Method.GET, url, Response.Listener { response ->

            Log.d("API RESPONSE: ", "Response in string: " + response)

            val stringResponse = response.toString()
            val jsonResponse = JSONObject(stringResponse)

        }, Response.ErrorListener {

            Toast.makeText(activity, it.toString(), Toast.LENGTH_LONG).show()

        }) {

            override fun getHeaders(): MutableMap<String, String> {

                val headers = HashMap<String, String>()

                val sharedPref: SharedPreferences = activity!!.getSharedPreferences(GET_SHARED_PREFERENCES, SHARED_PREFERENCES_PRIVATE_MODE)
                val tokenFromPreferences = sharedPref.getString(SHARED_PREFERENCES_TOKEN, "Default Token")

                headers["Verifytoken"] = tokenFromPreferences ?: "Default Token"

                return headers

            }

        }

        queue.add(stringRequest)

    }

Вот ответ в строке:

<pre>Array
    (
        [week1] => Array
            (
                [0] => Array
                    (
                        [PostID] => 4
                        [Title] => CoD
                        [Description] => dummy caption
                        [Tags] => dummy name
                        [UserID] => 1
                        [PostVideo] => uploads/post/videos/9248674463720200302113734.mp4
                        [PostImage] => 
                        [SortOrder] => 0
                        [Hide] => 0
                        [IsActive] => 1
                        [CreatedAt] => 1583951969
                        [UpdatedAt] => 1583148877
                        [CreatedBy] => 0
                        [UpdatedBy] => 0
                        [FullName] => 
                        [UserImage] => uploads/images/560345183620200226014232.png
                        [LikesCount] => 0
                        [IsLiked] => 0
                        [ReadCount] => 0
                    )

            )

        [week2] => Array
            (
                [0] => Array
                    (
                        [PostID] => 3
                        [Title] => CoD
                        [Description] => dummy caption
                        [Tags] => dummy name
                        [UserID] => 1
                        [PostVideo] => uploads/post/videos/4755111040320200302113134.mp4
                        [PostImage] => 
                        [SortOrder] => 0
                        [Hide] => 0
                        [IsActive] => 1
                        [CreatedAt] => 1583148871
                        [UpdatedAt] => 1583148871
                        [CreatedBy] => 0
                        [UpdatedBy] => 0
                        [FullName] => 
                        [UserImage] => uploads/images/560345183620200226014232.png
                        [LikesCount] => 2
                        [IsLiked] => 0
                        [ReadCount] => 0
                    )

                [1] => Array
                    (
                        [PostID] => 9
                        [Title] => CoD
                        [Description] => asd
                        [Tags] => xzc
                        [UserID] => 1
                        [PostVideo] => 
                        [PostImage] => 
                        [SortOrder] => 0
                        [Hide] => 0
                        [IsActive] => 1
                        [CreatedAt] => 1583164944
                        [UpdatedAt] => 1583164944
                        [CreatedBy] => 0
                        [UpdatedBy] => 0
                        [FullName] => 
                        [UserImage] => uploads/images/560345183620200226014232.png
                        [LikesCount] => 0
                        [IsLiked] => 0
                        [ReadCount] => 0
                    )

                [2] => Array
                    (
                        [PostID] => 5
                        [Title] => CoD
                        [Description] => dummy caption
                        [Tags] => dummy name
                        [UserID] => 1
                        [PostVideo] => uploads/post/videos/687237077420200302113834.mp4
                        [PostImage] => 
                        [SortOrder] => 0
                        [Hide] => 0
                        [IsActive] => 1
                        [CreatedAt] => 1583148878
                        [UpdatedAt] => 1583148878
                        [CreatedBy] => 0
                        [UpdatedBy] => 0
                        [FullName] => 
                        [UserImage] => uploads/images/560345183620200226014232.png
                        [LikesCount] => 0
                        [IsLiked] => 0
                        [ReadCount] => 0
                    )

                [3] => Array
                    (
                        [PostID] => 2
                        [Title] => 
                        [Description] => hamza update
                        [Tags] => 
                        [UserID] => 1
                        [PostVideo] => uploads/post/videos/3259382345320200302111833.mp4
                        [PostImage] => 
                        [SortOrder] => 0
                        [Hide] => 0
                        [IsActive] => 1
                        [CreatedAt] => 1583148798
                        [Upd

Ошибка произошла в строке "val jsonResponse = JSONObject (stringResponse)". Я попытался поместить JSONArray, но снова получил ту же ошибку

Вот ошибка:

--------- beginning of crash

2020-03-15 15: 02: 58,793 7721-7721 / com.example.GameX E / AndroidRuntime: ИСКЛЮЧИТЕЛЬНОЕ ИСКЛЮЧЕНИЕ: основной процесс: com.example.GameX, PID: 7721 java .lang.RuntimeException: java .lang.reflect.InvocationTargetException at com. android .internal.os.RuntimeInit $ MethodAndArgsCaller.run (RuntimeInit. java: 502) в com. android .internal.os. ZygoteInit.main (ZygoteInit. java: 930) Вызывается: java .lang.reflect.InvocationTargetException в java .lang.reflect.Method.invoke (собственный метод) в com. android .internal.os .RuntimeInit $ MethodAndArgsCaller.run (RuntimeInit. java: 492) в com. android .internal.os.ZygoteInit.main (ZygoteIn it. java: 930) Причина: org. json .JSONException: значение Массив типа java .lang.String не может быть преобразован в JSONObject в org. json. JSON .typeMismatch (JSON. java: 112) в орг. json .JSONObject. (JSONObject. java: 168) в орг. json .JSONObject. (JSONObject. java: 181) в com.example.GameX .homeScreenFragment $ getPosts $ stringRequest $ 2.onResponse (homeScreenFragment.kt: 65) в com.example.GameX.homeScreenFragment $ getPosts $ stringRequest $ 2.onResponse (homeScreenFragment.kt: 60) в com. android .volley.quolbox « . java: 102) в android .os.Handler.handleCallback (Обработчик. java: 883) в android .os.Handler.dispatchMessage (Обработчик. java: 100) в android. os.Looper.l oop (Looper. java: 214) в android .app.ActivityThread.main (ActivityThread. java: 7356) в java .lang.reflect.Method.invo ke (собственный метод) в com. android .internal.os.RuntimeInit $ MethodAndArgsCaller.run (RuntimeInit. java: 492) в com. android .internal.os.ZygoteInit.main (ZygoteInit. java) : 930)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...