Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Soyeong choi week5 #43

Open
wants to merge 1 commit into
base: soyeong-choi
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

<application
android:allowBackup="true"
android:usesCleartextTraffic="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
Expand Down
86 changes: 43 additions & 43 deletions app/src/main/java/com/example/gdsc/Adapter.kt
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
package com.example.gdsc

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import coil.api.load

class IntroductionAdapter :
ListAdapter<MemberDto, IntroductionAdapter.IntroductionViewHolder>(IntroductionDiffCallback()) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): IntroductionViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.item_introduction_gdsc_android, parent, false)
return IntroductionViewHolder(view)
}

override fun onBindViewHolder(holder: IntroductionViewHolder, position: Int) {
holder.bind(getItem(position)) //position에 해당하는 data type을 전달
}

class IntroductionViewHolder(private val view: View) : RecyclerView.ViewHolder(view) {
private val memberImage = view.findViewById<ImageView>(R.id.iv_member)
private val memberName = view.findViewById<TextView>(R.id.tv_member_name)
fun bind(member: MemberDto) {
memberImage.load(member.image)
memberName.text = member.name
}
}
}

class IntroductionDiffCallback : DiffUtil.ItemCallback<MemberDto>() {
override fun areItemsTheSame(oldItem: MemberDto, newItem: MemberDto): Boolean {
return oldItem.name == newItem.name
}

override fun areContentsTheSame(oldItem: MemberDto, newItem: MemberDto): Boolean {
return oldItem == newItem
}
}
//package com.example.gdsc
//
//import android.view.LayoutInflater
//import android.view.View
//import android.view.ViewGroup
//import android.widget.ImageView
//import android.widget.TextView
//import androidx.recyclerview.widget.DiffUtil
//import androidx.recyclerview.widget.ListAdapter
//import androidx.recyclerview.widget.RecyclerView
//import coil.api.load
//
//class IntroductionAdapter :
// ListAdapter<MemberDto, IntroductionAdapter.IntroductionViewHolder>(IntroductionDiffCallback()) {
// override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): IntroductionViewHolder {
// val view = LayoutInflater.from(parent.context)
// .inflate(R.layout.item_introduction_gdsc_android, parent, false)
// return IntroductionViewHolder(view)
// }
//
// override fun onBindViewHolder(holder: IntroductionViewHolder, position: Int) {
// holder.bind(getItem(position)) //position에 해당하는 data type을 전달
// }
//
// class IntroductionViewHolder(private val view: View) : RecyclerView.ViewHolder(view) {
// private val memberImage = view.findViewById<ImageView>(R.id.iv_member)
// private val memberName = view.findViewById<TextView>(R.id.tv_member_name)
// fun bind(member: MemberDto) {
// memberImage.load(member.image)
// memberName.text = member.name
// }
// }
//}
//
//class IntroductionDiffCallback : DiffUtil.ItemCallback<MemberDto>() {
// override fun areItemsTheSame(oldItem: MemberDto, newItem: MemberDto): Boolean {
// return oldItem.name == newItem.name
// }
//
// override fun areContentsTheSame(oldItem: MemberDto, newItem: MemberDto): Boolean {
// return oldItem == newItem
// }
//}
2 changes: 1 addition & 1 deletion app/src/main/java/com/example/gdsc/ApiFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import retrofit2.Retrofit

object ApiFactory {
private const val BASE_URL =
"https://gdsc-4f545-default-rtdb.asia-southeast1.firebasedatabase.app/"
"https://apis.data.go.kr/1360000/VilageFcstInfoService_2.0/"

val retrofit: Retrofit by lazy {
Retrofit.Builder()
Expand Down
13 changes: 7 additions & 6 deletions app/src/main/java/com/example/gdsc/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,23 @@ class MainActivity : AppCompatActivity() {
}

private fun getMemberApi() {
val adapter = IntroductionAdapter()
binding.rvIntroductionGdscAndroid.adapter = adapter
getMemberService.getMember().enqueue(object : retrofit2.Callback<List<MemberDto>> {
// val adapter = IntroductionAdapter()
// binding.rvIntroductionGdscAndroid.adapter = adapter
getMemberService.getMember().enqueue(object : retrofit2.Callback<MemberDto> {
override fun onResponse(
call: Call<List<MemberDto>>, response: Response<List<MemberDto>>
call: Call<MemberDto>, response: Response<MemberDto>
) {
if (response.isSuccessful) {
response.body()?.let { adapter.submitList(it) }
response.body()?.let { Log.d("result", it.toString()) }
} else {
Log.d("error", "실패한 응답")
}
}

override fun onFailure(call: Call<List<MemberDto>>, t: Throwable) {
override fun onFailure(call: Call<MemberDto>, t: Throwable) {
t.message?.let { Log.d("error", it) } ?: "서버통신 실패(응답값 X)"
}

})
}
}
65 changes: 61 additions & 4 deletions app/src/main/java/com/example/gdsc/Member.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,67 @@ import androidx.annotation.DrawableRes
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

//@Serializable
//data class MemberDto(
// @SerialName("img")
// val image: String,
// @SerialName("name")
// val name: String
//)
@Serializable
data class MemberDto(
@SerialName("img")
val image: String,
@SerialName("name")
val name: String
@SerialName("response")
val response: Response
)

@Serializable
data class Response(
@SerialName("header")
val header: Header,
@SerialName("body")
val body: Body
)

@Serializable
data class Header(
@SerialName("resultCode")
val resultCode: String,
@SerialName("resultMsg")
val resultMsg: String
)

@Serializable
data class Body(
@SerialName("dataType")
val dataType: String,
@SerialName("items")
val items: Items,
@SerialName("pageNo")
val pageNo: Int,
@SerialName("numOfRows")
val numOfRows: Int,
@SerialName("totalCount")
val totalCount: Int
)

@Serializable
data class Items(
@SerialName("item")
val item: List<Item>
)

@Serializable
data class Item(
@SerialName("baseDate")
val baseDate: String,
@SerialName("baseTime")
val baseTime: String,
@SerialName("category")
val category: String,
@SerialName("nx")
val nx: Int,
@SerialName("ny")
val ny: Int,
@SerialName("obsrValue")
val obsrValue: String
)
13 changes: 11 additions & 2 deletions app/src/main/java/com/example/gdsc/MemberApiService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@ package com.example.gdsc

import retrofit2.Call
import retrofit2.http.GET
import retrofit2.http.Query

interface MemberApiService {
@GET("member.json")
fun getMember(): Call<List<MemberDto>>
@GET("getUltraSrtNcst?serviceKey=IqVvPpU%2BF978g9EmI5OKfapy9GsFWMo%2F6kpq2NoOoeZvibCCxmdMpzBXKujzEON3%2F2i4%2FDa1trld4oEEb84y7w%3D%3D")
fun getMember(
@Query("numOfRows") numOfRows: Int = 1,
@Query("pageNo") pageNo: Int = 1,
@Query("dataType") dataType: String = "JSON",
@Query("base_date") base_date: String = "20231105",
@Query("base_time") base_time: String = "0600",
@Query("nx") nx: Int = 55,
@Query("ny") ny: Int = 127
): Call<MemberDto>
}
22 changes: 15 additions & 7 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,22 @@
android:layout_height="match_parent"
android:layout_gravity="center">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_introduction_gdsc_android"
<!-- <androidx.recyclerview.widget.RecyclerView-->
<!-- android:id="@+id/rv_introduction_gdsc_android"-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="match_parent"-->
<!-- app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"-->
<!-- app:layout_constraintEnd_toEndOf="parent"-->
<!-- app:layout_constraintStart_toStartOf="parent"-->
<!-- app:spanCount="2"-->
<!-- tools:listitem="@layout/item_introduction_gdsc_android" />-->

<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
android:layout_height="wrap_content"
android:text="서버 통신 연습"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:spanCount="2"
tools:listitem="@layout/item_introduction_gdsc_android" />
app:layout_constraintStart_toStartOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>