Skip to content

Commit

Permalink
Added the dog api
Browse files Browse the repository at this point in the history
  • Loading branch information
ayanchavand committed Dec 22, 2023
1 parent 18ff07e commit b5e01ad
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 11 deletions.
6 changes: 6 additions & 0 deletions app/src/main/java/com/example/app/DogData.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.example.app

data class DogData (
val message: String,
val status: String
)
29 changes: 25 additions & 4 deletions app/src/main/java/com/example/app/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.util.Log
import android.widget.Button
import android.widget.ImageView
import android.widget.Switch
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.bumptech.glide.Glide
import com.google.gson.Gson
Expand All @@ -24,25 +25,37 @@ class MainActivity : AppCompatActivity() {

val image: ImageView = findViewById(R.id.image)
val submitCat: Button = findViewById(R.id.submitCat)
val submitDog: Button = findViewById(R.id.submitDog)
val apiToggle: Switch = findViewById(R.id.apiToggle)

val apiText: TextView = findViewById(R.id.apiText)


submitCat.setOnClickListener {
Glide.with(this).clear(image)
if(apiToggle.isChecked()){
apiText.text = "API: cataas.com"
val imageUrl = "https://cataas.com/cat?timestamp=${System.currentTimeMillis()}"
Glide.with(this).load(imageUrl).into(image)
}

else{
apiText.text = "API: thecatapi.com"
val response = httpReq("null", "https://api.thecatapi.com/v1/images/search")
Log.d("LOG!", response)
val imageUrl = jsonParser(response)
val imageUrl = jsonParserCat(response)
Log.d("LOG!", imageUrl)
Glide.with(this).load(imageUrl).into(image)
}
}

submitDog.setOnClickListener {
apiText.text = "API: dog.ceo"
val response = httpReq("", "https://dog.ceo/api/breeds/image/random")
val imageUrl = jsonParserDog(response)
Log.d("LOG!", imageUrl)
Glide.with(this).load(imageUrl).into(image)

}
}
}

Expand Down Expand Up @@ -70,7 +83,7 @@ private fun httpReq(apiKey: String, url: String): String{
return jsonResponse
}

private fun jsonParser(responseString: String): String{
private fun jsonParserCat(responseString: String): String{
val gson = Gson()
val catDataArray = gson.fromJson(responseString, Array<CatData>::class.java)

Expand All @@ -84,4 +97,12 @@ private fun jsonParser(responseString: String): String{
// Handle the case when the array is null or empty
return ""
}
}
}

private fun jsonParserDog(responseString: String): String{
val gson = Gson()
val dogData = gson.fromJson(responseString, DogData::class.java)

return dogData.message;
}

45 changes: 38 additions & 7 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@

<Button
android:id="@+id/submitCat"
android:layout_width="137dp"
android:layout_height="63dp"
android:text="Cat"
android:layout_width="123dp"
android:layout_height="64dp"
android:text="Cats"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
Expand All @@ -46,13 +46,17 @@

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_width="158dp"
android:layout_height="27dp"
android:layout_marginTop="8dp"
android:textAlignment="center"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/submit"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.47"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/image" />
app:layout_constraintTop_toBottomOf="@+id/image"
app:layout_constraintVertical_bias="0.0" />

<Switch
android:id="@+id/apiToggle"
Expand All @@ -65,4 +69,31 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />

<TextView
android:id="@+id/apiText"
android:layout_width="190dp"
android:layout_height="23dp"
android:text="Find images to see the API used"
android:textAlignment="center"
android:textColor="@color/design_default_color_secondary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.977" />

<Button
android:id="@+id/submitDog"
android:layout_width="143dp"
android:layout_height="63dp"
android:text="Dogs"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@+id/apiText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/submitCat"
app:layout_constraintVertical_bias="0.406" />

</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit b5e01ad

Please sign in to comment.