Android okhttp - Запрос POST: проблемы с преобразованием ответа в правильный JSONArray - PullRequest
0 голосов
/ 21 июня 2020

Обычно я использую loopj AsyncHttpClient для API-интерфейсов, но, к сожалению, он не работает со старыми Android API-интерфейсами, использующими https, поэтому мне приходится использовать вместо него okhttp.

Это код:

val formBody = FormBody.Builder()
    .add("lastid", lastID.toString())
    .add("gal", gal)
    .build()
val request = Request.Builder()
    .url("$domain/bla.php")
    .post(formBody)
    .build()

client.newCall(request).execute().use { response ->
    if (!response.isSuccessful){
        Log.d(tagg,"Unexpected code $response")
    }

    val myResponse = JSONArray(response.body!!.string())

    Log.d(tagg, myResponse.toString())

    setMemes(myResponse, gal)
}

Иногда это срабатывает, JSONArray анализируется внутри setMemes, но иногда нет! Однако журнал ВСЕГДА показывает JSONArray. Итак, я предполагаю, что что-то не так с тем, как ответ конвертируется в JSONArray. НИКОГДА не проблема сделать это с AsyncHttpClient, потому что вам не нужно делать никаких дополнительных шагов для получения JSONArray, таких как преобразование

Есть ли ПРАВИЛЬНЫЙ способ получить JSONArray с okhttp, который работает каждый раз, а не случайным образом? ?

EDIT1:

Это JSONArray после использования loopj (не полностью помещается в logcat):

2020-06-21 16:07:14.605 22393-22533/? D/pikabo: [{"id":"140","title":"That must hurt after a while","pic":"atYsvcQx_gif_38.mp4","size":"320|240","userid":"17","nickname":"Lolhunt42","commentcount":"5","time":"1592414298","secondsago":334138,"checked":"1","liked":1,"disliked":0,"voteup":"4","votedown":"0"},{"id":"139","title":"","pic":"hWM9COQ180.jpg","size":"460|355","userid":"17","nickname":"Lolhunt42","commentcount":"0","time":"1592409945","secondsago":338491,"checked":"1","liked":1,"disliked":0,"voteup":"3","votedown":"0"},{"id":"138","title":"","pic":"VHyNissF49.jpg","size":"800|819","userid":"14","nickname":"Memetron1337","commentcount":"0","time":"1592348577","secondsago":399859,"checked":"1","liked":0,"disliked":0,"voteup":"1","votedown":"0"},{"id":"137","title":"Just like my dog lmao","pic":"RwuTkylV52.jpg","size":"852|986","userid":"14","nickname":"Memetron1337","commentcount":"0","time":"1592344259","secondsago":404177,"checked":"1","liked":1,"disliked":0,"voteup":"2","votedown":"0"},{"id":"134","title":"","pic":"GvXyE6Gw28.jpg","size":"800|874","userid":"14","nickname":"Memetron1337","commentcount":"1","time":"1592271317","secondsago":477119,"checked":"1","liked":0,"disliked":0,"voteup":"1","votedown":"0"},{"id":"133","title":"","pic":"jNq4uvqV30.jpg","size":"700|546","userid":"14","nickname":"Memetron1337","commentcount":"0","time":"1592271315","secondsago":477121,"checked":"1","liked":0,"disliked":0,"voteup":"2","votedown":"0"},{"id":"132","title":"","pic":"HCtosYx489.jpg","size":"716|770","userid":"14","nickname":"Memetron1337","commentcount":"1","time":"1592271312","secondsago":477124,"checked":"1","liked":1,"disliked":0,"voteup":"8","votedown":"1"},{"id":"131","title":"","pic":"NrNPzY7B98.jpg","size":"362|489","userid":"14","nickname":"Memetron1337","commentcount":"0","time":"1592269295","secondsago":479141,"checked":"1","liked":1,"disliked":0,"voteup":"3","votedown":"0"},{"id":"130","title":"","pic":"3MMU123B32.jpg","size":"640|669","userid":"14","nickname":"Memetron1337","commentcount":"1","time":"1592269292","secondsago":479144,"checked":"1","liked":1,"disliked":0,"voteup":"1","votedown":"0"},{"id":"129","title":"","pic":"HaHOC6W497.jpg","size":"720|922","userid":"14","nickname":"Memetron1337","commentcount":"0","time":"1592268759","secondsago":479677,"checked":"1","liked":0,"disliked":0,"voteup":"0","votedown":"0"},{"id":"128","title":"","pic":"kjVaL1zC85.jpg","size":"720|703","userid":"14","nickname":"Memetron1337","commentcount":"0","time":"1592268756","secondsago":479680,"checked":"1","liked":1,"disliked":0,"voteup":"2","votedown":"0"},{"id":"127","title":"","pic":"3g3YXzqM69.jpg","size":"700|607","userid":"14","nickname":"Memetron1337","commentcount":"0","time":"1592268754","secondsago":479682,"checked":"1","liked":0,"disliked":0,"voteup":"1","votedown":"0"},{"id":"126","title":"","pic":"cLGHi5YI19.jpg","size":"480|693","userid":"14","nickname":"Memetron1337","commentcount":"0","time":"1592268752","secondsago":479684,"checked":"1","liked":0,"disliked":0,"voteup":"1","votedown":"0"},{"id":"125","title":"","pic":"UNV5bIVc26.jpg","size":"711|738","userid":"14","nickname":"Memetron1337","commentcount":"0","time":"1592268747","secondsago":479689,"checked":"1","liked":1,"disliked":0,"voteup":"2","votedown":"0"},{"id":"124","title":"","pic":"x6zdb4SR02.jpg","size":"700|711","userid":"14","nickname":"Memetron1337","commentcount":"0","time":"1592265141","secondsago":483295,"checked":"1","liked":1,"disliked":0,"voteup":"2","votedown":"0"},{"id":"123","title":"","pic":"8UvBlGly68.jpg","size":"500|700","userid":"14","nickname":"Memetron1337","commentcount":"0","time":"1592265133","secondsago":483303,"checked":"1","liked":0,"disliked":0,"voteup":"1","votedown":"0"},{"id":"118","title":"Better than masks","pic":"I1Inh92348.jpg","size":"800|900","userid":"14","nickname":"Memetron1337","commentcount":"0","time":"1591727637","secondsago":1020799,"checked":"1","liked":1,"disliked":0,"voteup":"3","votedown":"0"},{"id":"107","title":"","pic":"jjMlP6Sf29.jpg","size":"719|845","userid":"14","nickname":"Memetron1337","commentcount

и это с использованием okhttp:

2020-06-21 16:07:24.576 22393-22536/? D/pikabo: [{"id":"140","title":"That must hurt after a while","pic":"atYsvcQx_gif_38.mp4","size":"320|240","userid":"17","nickname":"Lolhunt42","commentcount":"5","time":"1592414298","secondsago":334148,"checked":"1","liked":1,"disliked":0,"voteup":"4","votedown":"0"},{"id":"139","title":"","pic":"hWM9COQ180.jpg","size":"460|355","userid":"17","nickname":"Lolhunt42","commentcount":"0","time":"1592409945","secondsago":338501,"checked":"1","liked":1,"disliked":0,"voteup":"3","votedown":"0"},{"id":"138","title":"","pic":"VHyNissF49.jpg","size":"800|819","userid":"14","nickname":"Memetron1337","commentcount":"0","time":"1592348577","secondsago":399869,"checked":"1","liked":0,"disliked":0,"voteup":"1","votedown":"0"},{"id":"137","title":"Just like my dog lmao","pic":"RwuTkylV52.jpg","size":"852|986","userid":"14","nickname":"Memetron1337","commentcount":"0","time":"1592344259","secondsago":404187,"checked":"1","liked":1,"disliked":0,"voteup":"2","votedown":"0"},{"id":"134","title":"","pic":"GvXyE6Gw28.jpg","size":"800|874","userid":"14","nickname":"Memetron1337","commentcount":"1","time":"1592271317","secondsago":477129,"checked":"1","liked":0,"disliked":0,"voteup":"1","votedown":"0"},{"id":"133","title":"","pic":"jNq4uvqV30.jpg","size":"700|546","userid":"14","nickname":"Memetron1337","commentcount":"0","time":"1592271315","secondsago":477131,"checked":"1","liked":0,"disliked":0,"voteup":"2","votedown":"0"},{"id":"132","title":"","pic":"HCtosYx489.jpg","size":"716|770","userid":"14","nickname":"Memetron1337","commentcount":"1","time":"1592271312","secondsago":477134,"checked":"1","liked":1,"disliked":0,"voteup":"8","votedown":"1"},{"id":"131","title":"","pic":"NrNPzY7B98.jpg","size":"362|489","userid":"14","nickname":"Memetron1337","commentcount":"0","time":"1592269295","secondsago":479151,"checked":"1","liked":1,"disliked":0,"voteup":"3","votedown":"0"},{"id":"130","title":"","pic":"3MMU123B32.jpg","size":"640|669","userid":"14","nickname":"Memetron1337","commentcount":"1","time":"1592269292","secondsago":479154,"checked":"1","liked":1,"disliked":0,"voteup":"1","votedown":"0"},{"id":"129","title":"","pic":"HaHOC6W497.jpg","size":"720|922","userid":"14","nickname":"Memetron1337","commentcount":"0","time":"1592268759","secondsago":479687,"checked":"1","liked":0,"disliked":0,"voteup":"0","votedown":"0"},{"id":"128","title":"","pic":"kjVaL1zC85.jpg","size":"720|703","userid":"14","nickname":"Memetron1337","commentcount":"0","time":"1592268756","secondsago":479690,"checked":"1","liked":1,"disliked":0,"voteup":"2","votedown":"0"},{"id":"127","title":"","pic":"3g3YXzqM69.jpg","size":"700|607","userid":"14","nickname":"Memetron1337","commentcount":"0","time":"1592268754","secondsago":479692,"checked":"1","liked":0,"disliked":0,"voteup":"1","votedown":"0"},{"id":"126","title":"","pic":"cLGHi5YI19.jpg","size":"480|693","userid":"14","nickname":"Memetron1337","commentcount":"0","time":"1592268752","secondsago":479694,"checked":"1","liked":0,"disliked":0,"voteup":"1","votedown":"0"},{"id":"125","title":"","pic":"UNV5bIVc26.jpg","size":"711|738","userid":"14","nickname":"Memetron1337","commentcount":"0","time":"1592268747","secondsago":479699,"checked":"1","liked":1,"disliked":0,"voteup":"2","votedown":"0"},{"id":"124","title":"","pic":"x6zdb4SR02.jpg","size":"700|711","userid":"14","nickname":"Memetron1337","commentcount":"0","time":"1592265141","secondsago":483305,"checked":"1","liked":1,"disliked":0,"voteup":"2","votedown":"0"},{"id":"123","title":"","pic":"8UvBlGly68.jpg","size":"500|700","userid":"14","nickname":"Memetron1337","commentcount":"0","time":"1592265133","secondsago":483313,"checked":"1","liked":0,"disliked":0,"voteup":"1","votedown":"0"},{"id":"118","title":"Better than masks","pic":"I1Inh92348.jpg","size":"800|900","userid":"14","nickname":"Memetron1337","commentcount":"0","time":"1591727637","secondsago":1020809,"checked":"1","liked":1,"disliked":0,"voteup":"3","votedown":"0"},{"id":"107","title":"","pic":"jjMlP6Sf29.jpg","size":"719|845","userid":"14","nickname":"Memetron1337","commentcount

Выглядит абсолютно идентично

...