parent
bbc903e116
commit
66117fc62e
@ -0,0 +1,3 @@
|
||||
package api
|
||||
|
||||
class Data(val users: List<User>)
|
@ -0,0 +1,9 @@
|
||||
package api
|
||||
|
||||
import retrofit2.Call
|
||||
import retrofit2.http.GET
|
||||
|
||||
interface TetrisAPI {
|
||||
@GET("users/lists/xp")
|
||||
fun getAllUsers() : Call<UserResponse>
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
package api
|
||||
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.GsonBuilder
|
||||
import okhttp3.OkHttpClient
|
||||
import retrofit2.Call
|
||||
import retrofit2.Callback
|
||||
import retrofit2.Response
|
||||
import retrofit2.Retrofit
|
||||
import retrofit2.converter.gson.GsonConverterFactory
|
||||
|
||||
class TetrisClient(okHttpClient: OkHttpClient) {
|
||||
private val api: TetrisAPI
|
||||
|
||||
|
||||
init {
|
||||
// Create Gson object with lenient policy
|
||||
val gson = GsonBuilder()
|
||||
.setLenient()
|
||||
.create()
|
||||
|
||||
// Create Retrofit instance
|
||||
val retrofit = Retrofit.Builder()
|
||||
.baseUrl("https://ch.tetr.io/api/")
|
||||
.addConverterFactory(GsonConverterFactory.create(gson))
|
||||
.client(okHttpClient)
|
||||
.build()
|
||||
|
||||
|
||||
// Create API instance
|
||||
api = retrofit.create(TetrisAPI::class.java)
|
||||
}
|
||||
|
||||
fun getUsers(callback: (List<User>?, Throwable?) -> Unit) {
|
||||
api.getAllUsers().enqueue(object : Callback<UserResponse> {
|
||||
override fun onResponse(call: Call<UserResponse>, response: Response<UserResponse>) {
|
||||
if (response.isSuccessful) {
|
||||
// API call successful, parse response body
|
||||
val users = response.body()?.data?.users
|
||||
callback(users, null)
|
||||
} else {
|
||||
// API call failed, handle error
|
||||
val error = Exception("API call failed with code ${response.code()}")
|
||||
callback(null, error)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onFailure(call: Call<UserResponse>, t: Throwable) {
|
||||
// API call failed, handle error
|
||||
callback(null, t)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package api
|
||||
|
||||
class User(val id: String,
|
||||
val username: String,
|
||||
val role: String,
|
||||
val xp: Double,
|
||||
val supporter: Boolean,
|
||||
val verified: Boolean,
|
||||
val country: String,
|
||||
val timestamp: String,
|
||||
val gamesPlayed: Int,
|
||||
val gamesWon: Int,
|
||||
val gameTime: Int) {
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
package api
|
||||
|
||||
class UserResponse(val success: Boolean, val data: Data)
|
@ -0,0 +1,82 @@
|
||||
package but.androidstudio.tetris
|
||||
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.fragment.app.Fragment
|
||||
import api.TetrisAPI
|
||||
import api.TetrisClient
|
||||
import api.User
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.GsonBuilder
|
||||
import okhttp3.OkHttpClient
|
||||
import retrofit2.Call
|
||||
import retrofit2.Callback
|
||||
import retrofit2.Response
|
||||
import retrofit2.Retrofit
|
||||
import retrofit2.converter.gson.GsonConverterFactory
|
||||
import java.io.InputStream
|
||||
import java.security.KeyStore
|
||||
import java.security.SecureRandom
|
||||
import java.security.cert.CertificateFactory
|
||||
import javax.net.ssl.SSLContext
|
||||
import javax.net.ssl.TrustManagerFactory
|
||||
import javax.net.ssl.X509TrustManager
|
||||
|
||||
|
||||
class ClassementFragment : Fragment() {
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
}
|
||||
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater, container: ViewGroup?,
|
||||
savedInstanceState: Bundle?
|
||||
): View? {
|
||||
// Inflate the layout for this fragment
|
||||
return inflater.inflate(R.layout.fragment_classement, container, false)
|
||||
}
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
|
||||
// Read the SSL certificate for the request
|
||||
val certificateStream : InputStream = resources.openRawResource(R.raw.sni_cloudflaressl_com)
|
||||
|
||||
// We create a certificateFactory to extract de data of the certificateInputStream
|
||||
val certificateFactory = CertificateFactory.getInstance("X.509")
|
||||
val certificate = certificateFactory.generateCertificate(certificateStream)
|
||||
|
||||
//To specified the certificate to used
|
||||
val keyStore = KeyStore.getInstance(KeyStore.getDefaultType())
|
||||
keyStore.load(null, null)
|
||||
keyStore.setCertificateEntry("ca", certificate)
|
||||
val trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm())
|
||||
trustManagerFactory.init(keyStore)
|
||||
|
||||
//To create a SSL context
|
||||
val sslContext = SSLContext.getInstance("SSL")
|
||||
sslContext.init(null, trustManagerFactory.trustManagers, SecureRandom())
|
||||
|
||||
// We create a OkHttpClient to use the SSLContext
|
||||
val okHttpClient: OkHttpClient = OkHttpClient.Builder()
|
||||
.sslSocketFactory(sslContext.socketFactory, trustManagerFactory.trustManagers[0] as X509TrustManager)
|
||||
.build()
|
||||
|
||||
val client = TetrisClient(okHttpClient)
|
||||
|
||||
client.getUsers { users, error ->
|
||||
if (error != null) {
|
||||
// Handle error
|
||||
println("Error: ${error.message}")
|
||||
} else {
|
||||
// Handle success
|
||||
users?.forEach {
|
||||
println("User: ${it.username}")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context=".ClassementFragment">
|
||||
|
||||
<!-- TODO: Update blank fragment layout -->
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:text="@string/hello_blank_fragment" />
|
||||
|
||||
</FrameLayout>
|
Loading…
Reference in new issue