112 lines
4.5 KiB
Kotlin
112 lines
4.5 KiB
Kotlin
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
|
|
}
|
|
|
|
fun makeCommentary(token: String, videoId: Int, text: String): Boolean {
|
|
val con = this._newCon("videos/$videoId/comment-threads", "POST", token)
|
|
val params:String= "text=$text"
|
|
con.outputStream.write(params.toByteArray())
|
|
|
|
try {
|
|
if (con.responseCode == 200) {
|
|
return true
|
|
}
|
|
else{
|
|
Log.d("Status", con.responseMessage)
|
|
}
|
|
}
|
|
catch (err: Exception){
|
|
err.printStackTrace()
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
} |