Ajax petitions for commentaries

This commit is contained in:
ivan agosto 2019-01-25 11:38:37 -06:00
parent 9bf8ea0483
commit 5cf806577a
2 changed files with 97 additions and 2 deletions

View File

@ -0,0 +1,92 @@
package org.libre.agosto.p2play.ajax
import android.util.JsonReader
import android.util.JsonToken
import android.util.Log
import org.libre.agosto.p2play.models.CommentaryModel
import java.io.InputStreamReader
class Comments: Client() {
fun parseCommentaries(data: JsonReader): ArrayList<CommentaryModel> {
var commentaries = arrayListOf<CommentaryModel>()
data.beginObject()
while (data.hasNext()){
when(data.nextName()) {
"data" -> {
data.beginArray()
while (data.hasNext()) {
val comment = CommentaryModel()
data.beginObject()
while (data.hasNext()) {
val key = data.nextName()
when (key.toString()) {
"id" -> comment.id = data.nextInt()
"threadId" -> comment.threadId = data.nextInt()
"text" -> comment.commentary = data.nextString()
"totalReplies" -> comment.replies = data.nextInt()
"account" -> {
data.beginObject()
while (data.hasNext()){
val acKey = data.nextName()
when(acKey.toString()){
"displayName"-> comment.username=data.nextString()
"avatar"-> {
if(data.peek() == JsonToken.BEGIN_OBJECT){
data.beginObject()
while (data.hasNext()){
val avKey = data.nextName()
when(avKey){
"path"-> comment.userImageUrl = data.nextString()
else-> data.skipValue()
}
}
data.endObject()
}
else
data.skipValue()
}
"uuid" -> comment.userUuid = data.nextString()
"host" -> comment.userHost = data.nextString()
else -> data.skipValue()
}
}
data.endObject()
}
else -> data.skipValue()
}
}
data.endObject()
commentaries.add(comment)
}
data.endArray()
}
else -> data.skipValue()
}
}
data.endObject()
return commentaries
}
fun getCommentaries(videoId: Int): ArrayList<CommentaryModel> {
var commentaries = arrayListOf<CommentaryModel>()
val con = this._newCon("videos/$videoId/comment-threads", "GET")
try {
if (con.responseCode == 200) {
var response = InputStreamReader(con.inputStream)
var data = JsonReader(response)
commentaries = parseCommentaries(data)
}
} catch(err:Exception){
err?.printStackTrace()
Log.d("TypeErr",err?.message ,err.cause)
Log.d("Error","fallo la coneccion")
}
return commentaries
}
}

View File

@ -2,8 +2,11 @@ package org.libre.agosto.p2play.models
class CommentaryModel ( class CommentaryModel (
var id: Int = 0, var id: Int = 0,
var uuid: Int = 0, var threadId: Int = 0,
var userUuid: String = "",
var username: String = "", var username: String = "",
var userImageUrl: String = "", var userImageUrl: String = "",
var commentary: String = "" var commentary: String = "",
var userHost: String = "",
var replies: Int = 0
) )