Skip to content

NaHui999/android_assign

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

6 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

android_assign

๐Ÿ”ฅ์ฒซ๋ฒˆ์งธ ์„ธ๋ฏธ๋‚˜(10/10)

-:blue_heart: โ€‹ํ•„์ˆ˜๊ณผ์ œ(์™„๋ฃŒ:10/17)

1.MainActivity ๋กœ๊ทธ์ธ ํ™”๋ฉด์˜ ๋ชจ์Šต์ด๋‹ค. ํšŒ์›๊ฐ€์ž… ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด 2.secondActivity๋กœ ๋„˜์–ด๊ฐ„๋‹ค. ๋„˜์–ด๊ฐˆ๋•Œ๋Š” intent๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

MainActivity

2.SecondActivity ํšŒ์› ๊ฐ€์ž… ํ™”๋ฉด์œผ๋กœ SignUpActivity์ด๋‹ค. EditTextView์—์„œ inputType์„ ๋ณ€ํ™”์‹œํ‚ค๋ฉด password๋ถ€๋ถ„์˜ ๊ธ€์ž๊ฐ€ ์•ˆ๋ณด์ด๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค. hint์†์„ฑ์„ ์ ์šฉํ•˜๋ฉด ์ž…๋ ฅ์ด ์—†๋Š” ์ƒํƒœ์—์„œ ๋ฏธ๋ฆฌ๋ณด๊ธฐ ๊ธ€์”จ๊ฐ€ ๋‚˜ํƒ€๋‚œ๋‹ค.

#๊ธฐ๋ณธํ™”๋ฉด์˜ ๋ชจ์Šต

SecondActivity

ํ•˜๋‚˜๋ผ๋„ ์ž…๋ ฅ์ด ์•ˆ๋˜์—ˆ์„ ๋•Œ Toast๋ฉ”์„ธ์ง€๋กœ ์ž…๋ ฅ๋˜์ง€ ์•Š์€ ๊ฐ’์ด ์žˆ์Œ์„ ์•Œ๋ ค์ค€๋‹ค.

#์‹คํŒจ ํ† ์ŠคํŠธ๋ฉ”์„ธ์ง€

second_fail

๋ชจ๋“  input์ด ๋“ค์–ด์˜จ๋‹ค์Œ ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ ํšŒ์›๊ฐ€์ž… ์„ฑ๊ณต toast๋ฉ”์„ธ์ง€๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

#์„ฑ๊ณต ํ† ์ŠคํŠธ ๋ฉ”์„ธ์ง€

second_success

(๊ณผ์ œ์™„๋ฃŒ 10/17)- ๋ณด์™„ ํ•„์š”/readme์— ์ฝ”๋“œ๋ธ”๋Ÿญ ๋„ฃ๋Š”๋ฒ• ๋ฐฐ์šฐ๊ธฐ->ํ™•์ธ

-:tiger: : โ€‹์‹ฌํ™”๊ณผ์ œ 1 (๊ณผ์ œ์™„๋ฃŒ 10/29) ํšŒ์›๊ฐ€์ž… ์ดํ›„ mainํ™”๋ฉด์œผ๋กœ ๋Œ์•„๊ฐ€๋ฉด id์™€ password๊ฐ€ ์ž…๋ ฅ๋œ ์ƒํƒœ์ด๋‹ค. startActivityForResult์„ ํ™œ์šฉํ•˜์˜€๊ณ , onActivityResult๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œํ•˜์—ฌ ๋‚ด๋ถ€ ์„ค์ •์„ ๋ฐ”๊ฟจ๋‹ค. id์™€ pw๋ฅผ ์ด์ „ ํ™”๋ฉด์—์„œ ๋ฐ์ดํ„ฐ ๊ฐ’์„ ๋ฐ›์•„์™€์„œ setText๋ฅผ ํ•œ๋‹ค.

-์‚ฌ์ง„ ์‚ฌ์ด์ฆˆ ์ค„์ด๋Š” ๋ฐฉ๋ฒ•์„ ๋‹ค์‹œ ์ฐพ์•„๋ด์•ผํ•จ-> <img src="" width=""๋กœ ๊ฐ€๋Šฅ

-:tiger: ์‹ฌํ™”๊ณผ์ œ 2 ( ๋ฏธ์™„: ) sharedPreferences๋ฅผ ์‚ฌ์šฉํ•ด ์ž๋™๋กœ๊ทธ์ธ์„ ๊ตฌํ˜„ํ•œ๋‹ค. ์ž๋™๋กœ๊ทธ์ธ ๋ฒ„ํŠผ์„ ๋”ฐ๋กœ ์ถ”๊ฐ€ํ•˜์˜€๋‹ค.

๐Ÿ”ฅ๋‘๋ฒˆ์งธ ์„ธ๋ฏธ๋‚˜(10/10)

-:blue_heart: ํ•„์ˆ˜๊ณผ์ œ (๊ณผ์ œ์™„๋ฃŒ:10/30)

๐Ÿ’ฆ ์—๋Ÿฌ์ƒํ™ฉ >> โ€‹ ๋‘๋ฒˆ์งธ ์„ธ๋ฏธ๋‚˜ ํ•„์ˆ˜ ๊ณผ์ œ ๋‹จ๋…์œผ๋กœ๋Š” ์‹คํ–‰์ด ๋˜๋Š”๋ฐ, 1์ฃผ์ฐจ ๊ณผ์ œ์™€ ํ•จ๊ป˜ ๋ถ™์ด๋ฉด (์ „์ฒด ํ•ฉ๋ณธ์—์„œ) RecyclerView์—์„œ Recycler_detail๋กœ ๋„˜์–ด๊ฐ€๋Š” ๊ณผ์ •์—์„œ ์ค‘๊ฐ„์— ๋ฉˆ์ถ”๋Š” ์ƒํ™ฉ์ด ๋ฐœ์ƒํ•œ๋‹ค. ์–ด๋–ค ์š”์ธ์ด ๋ฌธ์ œ์ธ์ง€ ํ™•์ธ์ด ํ•„์š”ํ•จ.

๋‘๋ฒˆ์งธ ํ•„์ˆ˜ ๊ณผ์ œ ๋‹จ๋… ์‹คํ–‰์€ https://github.com/NaHui999/2nd_Seminar ์— ์˜ฌ๋ ค๋‘์—ˆ์Œ.

3์ฃผ์ฐจ ๊ณผ์ œ๋ฅผ ํ•˜๋ฉฐ Recylcer fragment๋ฅผ ๋งŒ๋“ค๋•Œ appbar๋ฅผ ๋”ฐ๋กœ ๋งŒ๋“ค ์ˆ˜ ์—†์–ด, ํ”„๋ž˜๊ทธ๋จผํŠธ์—” linear๋งŒ ๋ฐฐ์น˜ํ•˜์˜€์Œ. grid ์™€ linear ์˜ค๊ฐ€๋Š” ์‹ฌํ™”๊ณผ์ œ 1๋˜ํ•œ ์œ„ ๋งํฌ์—์„œ ํ™•์ธ ๊ฐ€๋Šฅํ•จ.

item_list_recycler.xml์„ ๋‘ฌ์„œ ๊ณ„์† ๋ฐ˜๋ณต ์‚ฌ์šฉ๋  ์•„์ดํ…œ ๋ ˆ์ด์•„์›ƒ์„ ๋‘”๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ฐ ์•„์ดํ…œ์— ๋“ค์–ด๊ฐ€๋Š” ๋ฐ์ดํ„ฐ๋ฅผ RecyclerData๋กœ ๋‘์—ˆ์œผ๋ฉฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋ทฐ์— ๋„ฃ์–ด์ฃผ๋Š” ์—ญํ• ์ด RecyclerViewHolder์ด๊ณ , ์•„์ดํ…œ ๋ณ„ ๋ทฐ ํ™€๋”๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๋ฐ์ดํ„ฐ๋ฅผ ์—ฎ๋Š” ์—ญํ• ์€ RecyclerAdapter์ด๋‹ค.

๋‚˜์˜ ํ”„๋กœํ•„์„ Recycler๋ทฐ๋กœ ๋งŒ๋“ ๋‹ค์Œ Adapter์— intent๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด์•„ ๋ณด๋‚ด์„œ Recycler_detail ์ฐฝ์„ ๋งŒ๋“ค์–ด ์ž์„ธํ•œ ๋‚ด์šฉ์„ ํ‘œ์‹œํ•ด์ค€๋‹ค.

class MainActivity : AppCompatActivity() {
    private lateinit var RecyclerAdapter: RecyclerAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        RecyclerAdapter = RecyclerAdapter(this)

        main_rcv.adapter = RecyclerAdapter
        main_rcv.layoutManager = LinearLayoutManager(this) //์ด ๋ ˆ์ด์•„์›ƒ์„ ๊ฒฉ์žํ˜•์œผ๋กœ๋“  ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋‹ค.

        RecyclerAdapter.data = mutableListOf(
                RecyclerData("์ด๋ฆ„","๋‚˜ํฌ์ •","2020/10/1","์ด๋ฆ„์— ๋Œ€ํ•œ ์ž์„ธํ•œ ์„ค๋ช…"),
                RecyclerData("๋‚˜์ด","22","2020/10/2","๋‚˜์ด์— ๋Œ€ํ•œ ์ž์„ธํ•œ ์„ค๋ช…"),
                RecyclerData("ํŒŒํŠธ","์•ˆ๋“œ๋กœ์ด๋“œ","2020/10/3","ํŒŒํŠธ์— ๋Œ€ํ•œ ์ž์„ธํ•œ ์„ค๋ช…"),
                RecyclerData("GitHub","www.github.com/nahui999","2020/10/4","๊นƒํ—™์— ๋Œ€ํ•œ ์ž์„ธํ•œ ์„ค๋ช…"),
                RecyclerData("Blog","None","2020/10/5","๋ธ”๋กœ๊ทธ์— ๋Œ€ํ•œ ์ž์„ธํ•œ ์„ค๋ช…"),
                RecyclerData("Sopt","www.sopt.org","2020/10/6","์†ํŠธ์— ๋Œ€ํ•œ ์ž์„ธํ•œ ์„ค๋ช…")
        )
        RecyclerAdapter.notifyDataSetChanged()

    }
}

์œ„๋Š” recycler ์ฐฝ์˜ ๋ชจ์Šต. Adapter๋ฅผ ๋ถˆ๋Ÿฌ์˜จ๋‹ค.

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewHolder {
    
        val view=LayoutInflater.from(context).inflate(R.layout.item_list_recycler,parent,false)
        return RecyclerViewHolder(view)
    }

    override fun getItemCount(): Int {
        return data.size 
    }

    override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int) {
        holder.onBind(data[position])

        holder.itemView.setOnClickListener{

            val intent=Intent(holder.itemView.context,Activity_Recycler_Detail::class.java)

            intent.putExtra("title",data[position].title)
            intent.putExtra("subtitle",data[position].subTitle)
            intent.putExtra("date",data[position].date)
            intent.putExtra("add",data[position].add)

            startActivity(holder.itemView.context,intent,null)
        }

RecyclerAdapter๋Š” onCreateViewHolder์™€ getItemCount, onBindViewHolder๋ฅผ ํ•„์ˆ˜์ ์œผ๋กœ ์˜ค๋ฒ„๋ผ์ด๋“œ ํ•ด์•ผํ•œ๋‹ค.

class RecyclerViewHolder (itemView:View):RecyclerView.ViewHolder(itemView){//val,varํ•˜๋ฉด ์„ ์–ธ๊ณผ ๋™์‹œ์— ์ดˆ๊ธฐํ™”.
private val title:TextView=itemView.findViewById(R.id.item_title) //๋ฐ”์ธ๋”ฉ์‹œํ‚ด.
    private val subTitle:TextView=itemView.findViewById(R.id.item_subtitle)

    fun onBind(data: RecyclerData){//Recyclerdata.kt๊ฐ€ ๊ฐ์ฒด๋กœ ๋“ค์–ด์˜ค๊ฒŒ๋จ.
        title.text=data.title
        subTitle.text=data.subTitle
    }
}

recyclerViewHolder๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ”์ธ๋”ฉ์„ ์‹œํ‚ค๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.

-:tiger: : โ€‹์‹ฌํ™”๊ณผ์ œ1 ( ์™„์„ฑ: 11/1)

grid์™€ linear layout์„ ์™”๋‹ค๊ฐ”๋‹ค ํ• ์ˆ˜ ์žˆ๋„๋ก appbar์„ ์ถ”๊ฐ€ํ•˜์—ฌ ์ง„ํ–‰ํ•˜์˜€์Œ.

3์ฃผ์ฐจ ๊ณผ์ œ๋ฅผ ํ•˜๋ฉฐ appbar๋ฅผ fragment์— ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์— ์–ด๋ ค์›€์„ ๊ฒช์–ด ๋”ฐ๋กœ ๋ถ„๋ฆฌํ•˜์—ฌ repository๋ฅผ ์ƒ์„ฑํ•จ.

๋งํฌ: https://github.com/NaHui999/2nd_Seminar

-:tiger: :์‹ฌํ™”๊ณผ์ œ2 (๋ฏธ์™„)

๐Ÿ”ฅ์„ธ๋ฒˆ์งธ ์„ธ๋ฏธ๋‚˜(10/10)

-:blue_heart: ํ•„์ˆ˜๊ณผ์ œ (์™„์„ฑ: 11/5)

Activity ์œ„์— ์—ฌ๋Ÿฌ๊ฐœ์˜ fragment๋ฅผ ๋„์›Œ ๊ฐœ๋ณ„ ์ž‘๋™ํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Œ.

-> ์ค‘๋ณต ํ”ผํ•˜๊ธฐ์œ„ํ•œ ๋ฐฉ๋ฒ•

ํ”„๋ž˜๊ทธ๋จผํŠธ์—” onCreate(), onCreateView(), on ActivityCreated()๊ฐ€ ์žˆ๋‹ค.

์—ฌ๊ธฐ์„œ๋Š” onCreateView()๋งŒ ์‚ฌ์šฉํ•œ๋‹ค.

1

2

๋™์˜์ƒ์ด ์ถ”๊ฐ€๊ฐ€ ์•ˆ๋˜์–ด์„œ ์šฐ์„  ์Šคํฌ๋ฆฐ์ƒท์œผ๋กœ ๋Œ€์ฒด

"""

class FirstFragment : Fragment() {
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        val view = inflater.inflate(R.layout.fragment_first, container, false)

        return view
    }
}

"""

์ด ๊ณผ์ œ์—์„œ๋Š” Activity1๊ฐœ์™€, Profile/Recycler/Empty Fragment์™€

ProfileFragment ๋‚ด์˜ Info์™€ Other Fragment๋กœ ์ด๋ฃจ์–ด์ง„๋‹ค.

Viewpager๋ฅผ ์ด์šฉํ•ด ์Šฌ๋ผ์ด๋“œ ํ˜•์‹์œผ๋กœ ๋ณด์—ฌ์ค„์ˆ˜ ์žˆ๋Š”๋ฐ, ViewPagerAdapter๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค.

""""

class ViewPagerAdapter(fm: FragmentManager): FragmentStatePagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT){
    var fragments= listOf<Fragment>()
    override fun getItem(position:Int): Fragment=when(position){
        0->FirstFragment()
        1->SecondFragment()
        else->throw IllegalStateException("Unexpected position &position")
    }

    override fun getCount():Int =2

    //override fun getItem(position: Int): Fragment =fragments[position]
    //override fun getCount():Int=fragmets.size

}

"""

์œ„ ๋ฐฉ์‹์œผ๋กœ ๊ฐ’์„ ๋„˜๊ฒจ์ฃผ๊ฒŒ ๋˜๋ฉด ViewPagerAdapter๋Š” 2๊ฐœ ๋งŒ๋“ค์–ด์ ธ์•ผํ•œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์—‘ํ‹ฐ๋น„ํ‹ฐ์—์„œ ์•„๋ž˜์ฒ˜๋Ÿผ Adapter๋ฅผ ํ˜ธ์ถœํ•ด์ฃผ์–ด์„œ ์ ์šฉํ•œ๋‹ค.

"""

class ProfileActivity : AppCompatActivity() {
    private lateinit var btmViewpageradapter: Btm_ViewPagerAdapter

    override fun onCreate(savedInstanceState: Bundle?){
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_profile)//๋‚˜์ค‘์— activity_profile ๋กœ ๋ฐ”๊ฟ”์•ผํ•จ.

        //BTM ViewPager ๋„˜๊ธฐ๊ธฐ
        btmViewpageradapter=Btm_ViewPagerAdapter(supportFragmentManager)
        btmViewpageradapter.fragments= listOf(
            AccountFragment(),
            RecyclerFragment(),
            EmptyFragment()
        )

        sample_bottom_viewpager.adapter=btmViewpageradapter


        //๋ทฐํŽ˜์ด์ €๋ฅผ ์Šฌ๋ผ์ด๋“œ ํ–ˆ์„๋•Œ ๊ทธ์— ๋Œ€์‘๋˜๋Š” ํ•˜๋‹จ ํƒญ ๋ณ€๊ฒฝ
        sample_bottom_viewpager.addOnPageChangeListener(object: ViewPager.OnPageChangeListener{
            override fun onPageScrollStateChanged(state: Int){
            }

            override fun onPageScrolled(
                position: Int,
                positionOffset:Float,
                positionOffsetPixels:Int
            ){}

            override fun onPageSelected(position:Int){
                sample_bottom_navi.menu.getItem(position).isChecked=true
            }
        })

        //ํ•˜๋‹จํƒญ์„ ๋ˆŒ๋ €์„๋•Œ ๋ทฐํŽ˜์ด์ € ํ™”๋ฉด ๋ณ€๊ฒฝ
        sample_bottom_navi.setOnNavigationItemSelectedListener {
            var index by Delegates.notNull<Int>()

            when(it.itemId){
                R.id.menu_account->index=0 //์ฒซ ํ™”๋ฉด์€ ์ธ๋ฑ์Šค ๊ธฐ์ค€ 0๋ฒˆ์งธ
                R.id.menu_recy->index=1
                R.id.menu_msg->index=2
            }
            sample_bottom_viewpager.currentItem=index
            true //๋ถˆ๋ฆฐ ๋ฐ˜ํ™˜
        }

    }

""""

Profile/Recycler/Empty Fragment ๋Š” BottomNavigator๋ฅผ ์‚ฌ์šฉํ•ด ํ•˜๋‹จํƒญ์œผ๋กœ ์Šฌ๋ผ์ด๋“œ๋‚˜ ํ„ฐ์น˜ ๋ฐฉ์‹์œผ๋กœ ์ด๋™ํ•œ๋‹ค. ์œ„ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด,

addOnPageChangeListener์™€ setOnNavigationItemSelectedListener๊ฐ€ ๊ทธ ์—ญํ• ์„ ํ•œ๋‹ค.

ProfileFragment ๋‚ด์˜ ํ”„๋ ˆ๊ทธ๋จผํŠธ๋Š” : Tab Layout์„ ์‚ฌ์šฉํ•œ๋‹ค.

ProfileFragment ๋‚ด๋ถ€์—์„œ ์ž‘๋™ํ•˜๋ฏ€๋กœ ProfileFragment.kt์—

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    viewPagerAdapter = ViewPagerAdapter(childFragmentManager)
    viewPagerAdapter.fragments = listOf(
        FirstFragment(),
        SecondFragment()
    )

    sample_tab_viewpager.adapter=viewPagerAdapter

    sample_tab.setupWithViewPager(sample_tab_viewpager)
    sample_tab.apply{
        getTabAt(0)?.text="INFO"
        getTabAt(1)?.text="OTHER"
    }
}

๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

๐Ÿฏ : โ€‹์‹ฌํ™”๊ณผ์ œ_ ์—†์—ˆ์Œ.

4๋ฒˆ์งธ ์„ธ๋ฏธ๋‚˜_ ๋””์ž์ธ ์„ธ๋ฏธ๋‚˜

5๋ฒˆ์งธ ์„ธ๋ฏธ๋‚˜_ ํด๋””ํ•ฉ๋™

๐Ÿ”ฅ์—ฌ์„ฏ๋ฒˆ์งธ ์„ธ๋ฏธ๋‚˜(11/21)

-:blue_heart: ํ•„์ˆ˜๊ณผ์ œ (์™„์„ฑ: 12/4)

postman์œผ๋กœ ์„œ๋ฒ„ ํ™•์ธ.

[sign-up]

Cap 2020-11-23 01-16-28-041

[sign in]

Cap 2020-11-23 01-16-59-922

<ํ•„์ˆ˜ ๊ณผ์ œ ์ „์ฒด ๋ชจ์Šต.gif>

์ฝ”๋“œ์„ค๋ช…

module gradle์— ์ถ”๊ฐ€

// https://github.com/square/retrofit
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
// Retrofit ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์‘๋‹ต์œผ๋กœ ๊ฐ€์งœ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•จ
implementation 'com.squareup.retrofit2:retrofit-mock:2.9.0'
// https://github.com/google/gson
implementation 'com.google.code.gson:gson:2.8.6'
// Retrofit์—์„œ Gson์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'

Manifest ํŒŒ์ผ์— ์ถ”๊ฐ€

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
    android:usesCleartextTraffic="true"

์ธํ„ฐํŽ˜์ด์Šค ์„ค๊ณ„

Headers๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ๊ณ , POST์ธ์ง€ GET์ธ์ง€ ๋ช…์‹œํ•ด์ค€๋‹ค.

interface SoptService{

    @Headers("Content-Type:application/json")
    @POST("/users/signin")
    fun postLogin(
        @Body body : RequestLoginData
    ) : Call<ResponseLoginData>
}

Call์€ ๋น„๋™๊ธฐ ํ†ต์‹ ์„ ๋„์™€์ฃผ๋Š” Retrofit ๊ฐ์ฒด์ด๊ณ , ์„œ๋ฒ„์—์„œ ๋ฐ›์•„์˜ฌ Response๊ฐ์ฒด๋ฅผ ๋ช…์‹œํ•ด์ค€๋‹ค. ๋ฆฌ์ŠคํŠธ ๊ฐ์ฒด์ธ๊ฒฝ์šฐ List๋ผ ์“ด๋‹ค

Request์™€ Response Dataํ˜•ํƒœ๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

JSONํŒŒ์ผ์„ Kotlin Data Class ํ˜•ํƒœ๋กœ ๋งŒ๋“ค์–ด์ฃผ๋Š” Plugin์„ ์ถ”๊ฐ€ํ•˜์˜€๋‹ค.

data class RequestLoginData(
    val email : String,
    val password :String
)
data class ResponseLoginData(
    val data: Data,
    @SerializedName("message")
    val responseMessage: String,
    val status: Int,
    val success: Boolean
){
    data class Data(
        val email:String,
        val password: String,
        val userName: String
    )
}

JSON ๊ฐ์ฒด์˜ ํ‚ค๊ฐ’๊ณผ ํƒ€์ž…์„ ๋งž์ถฐ์ค€๋‹ค. @SerializedName ์–ด๋…ธํ…Œ์ด์…˜์„ ํ†ตํ•ด JSON ๊ฐ์ฒด์˜ ๊ฐ’๊ณผ ๋Œ€์‘์‹œํ‚ฌ์ˆ˜์žˆ๋‹ค. JSON์—๋Š” "message":"๋กœ๊ทธ์ธ์„ฑ๊ณต."์ฒ˜๋Ÿผ ๋‚˜์™€์žˆ๋‹ค.

Interface๋ฅผ ์‹ค์ œ ๊ตฌํ˜„์ฒด๋กœ ๋งŒ๋“ ๋‹ค.

object SoptServiceImpl {
    private const val BASE_URL = "http://15.164.83.210:3000"
    private val retrofit : Retrofit = Retrofit.Builder()
        .baseUrl(BASE_URL)
        .addConverterFactory(GsonConverterFactory.create())
        .build()
    val service : SoptService = retrofit.create(SoptService::class.java)

}

์‹ฑ๊ธ€ํ†ค ๊ฐ์ฒด์ด๋ฏ€๋กœ Objectํ˜•ํƒœ์ด๋‹ค.

BaseURL์„ ์ ์„ ๋–„๋Š” /๋ฅผ ์กฐ์‹ฌํ•ด์•ผํ•œ๋‹ค. ์œ„์˜ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์„ค๊ณ„ํ• ๋•Œ /๋ฅผ ์ผ๋‹ค๋ฉด ์—ฌ๊ธฐ์„œ๋Š” ์“ฐ๋ฉด ์•ˆ๋œ๋‹ค. ๊ฒน์น˜๋ฉด ์ธ์‹ ๋ชปํ•จ.

retrofit ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋‹จ๊ณ„๋กœ Builder๋Š” ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์ด๊ณ , baseUrl()์€ ๋นŒ๋”๊ฐ์ฒด์˜ URL์„ ํ˜ธ์ถœํ•œ๋‹ค. ์„œ๋ฒ„์˜ main url ์ „๋‹ฌํ•œ๋‹ค.

add~ ๋Š” gson์„ ์—ฐ๋™ํ•˜๋Š” ์—ญํ• ์„ํ•œ๋‹ค. (๋ ˆํŠธ๋กœํ• ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃจ๊ธฐ ์‰ฝ๊ฒŒ), build๋Š” ๋ฐ˜ํ™˜ํ•œ๋‹ค.

์›๋ž˜ Activity์—์„œ Callback ๋“ฑ๋กํ•˜์—ฌ ํ†ต์‹ ์š”์ฒญ

login_btn.setOnClickListener{
    id=id_input.text.toString()
    pw=pw_input.text.toString()

    val call: Call<ResponseLoginData> = SoptServiceImpl.service.postLogin(
        RequestLoginData(email = id, password = pw)
    )
    call.enqueue(object : Callback<ResponseLoginData> {
        override fun onFailure(call: Call<ResponseLoginData>, t: Throwable) {
            //ํ†ต์‹  ์‹คํŒจ ๋กœ์ง
            Toast.makeText(this@MainActivity, "๋กœ๊ทธ์ธ ์‹คํŒจ", Toast.LENGTH_LONG).show()
        }

        override fun onResponse(
            call: Call<ResponseLoginData>,
            response: Response<ResponseLoginData>
        ) {
            response.takeIf { it.isSuccessful }
                ?.body()
                ?.let { it ->
                    it.data.let { data ->
                        Toast.makeText(this@MainActivity,"${data.userName} ๋‹˜ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค.",Toast.LENGTH_LONG).show()
                    }

                } ?: showError(response.errorBody())
        }

        private fun showError(error: ResponseBody?) {
            val e = error ?: return
            val ob = JSONObject(e.string())
            Toast.makeText(this@MainActivity, ob.getString("message"), Toast.LENGTH_LONG)
                .show()
        }
    })
}

์—ฌ๊ธฐ์„œ๋Š” ๋กœ๊ทธ์ธ ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ ์ˆ˜ํ–‰ํ•˜๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด setonClickListener ์•ˆ์— call์„ ๋„ฃ์—ˆ๋‹ค.

onFailure๋Š” ๋„คํŠธ์›Œํฌ ์—ฐ๊ฒฐ์ด ์•ˆ๋˜์—ˆ์„ ๋•Œ ๋ฐœ์ƒํ•˜๋ฏ€๋กœ, ๋„คํŠธ์›Œํฌ ๊ผญ ํ™•์ธํ•˜์ž...^^

onResponse๋Š” ์ •์ƒ์ˆ˜ํ–‰๋œ ๊ฒฝ์šฐ๋กœ data๋ฅผ ๋ฐ›์•„์™€์„œ userName์„ Toast๋ฉ”์„ธ์ง€๋กœ ์•Œ๋ ค์ค€๋‹ค.

ShowError๋Š” ๋„คํŠธ์›Œํฌ๋Š” ์ •์ƒ ์—ฐ๊ฒฐ์ด์ง€๋งŒ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ• ๋•Œ ๋ฌธ์ œ ์ƒํ™ฉ์„ ์•Œ๋ ค์ค€๋‹ค.

-:tiger: : ์‹ฌํ™”๊ณผ์ œ1 ( ์™„์„ฑ: 12/11)

Reqres - A hosted REST-API ready to respond to your AJAX requests

reqres.in ์‚ฌ์ดํŠธ์—์„œ ๋”๋ฏธ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์™€์„œ ์ง„ํ–‰.

์ „์ฒด์ ์ธ ๊ตฌํ˜„์€ ์œ„์™€ ๊ฐ™์œผ๋ฏ€๋กœ ํ—ท๊ฐˆ๋ ธ๋˜ ๋ถ€๋ถ„๋งŒ ๋ช…์‹œ.( ํ‹€๋ฆฐ ๋ถ€๋ถ„์„ ์•Œ๋ ค์ค€ ๋‹ค๋นˆ์–ธ๋‹ˆ ๋•กํ:blue_heart: )

Glide๋ฅผ ์‚ฌ์šฉํ•ด avatar, ์ฆ‰ ๊ทธ๋ฆผ์„ ๋ฐ›์•„์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

Glide.with(itemView)
        .load(data.avatar)
        .placeholder(R.drawable.earth)
        .error(R.drawable.earth)
        .into(avatar)

placeholder๋Š” ๋กœ๋”ฉ์ค‘ ๊ทธ๋ฆผ์ด๊ณ , error๋Š” ์—๋Ÿฌ๋ฐœ์ƒ์‹œ ๊ทธ๋ฆผ์ด๋‹ค. avatar๋ผ๋Š” xml ์‚ฌ์ง„ ์นธ์— ๋“ค์–ด๊ฐ„๋‹ค.

itemView๋ถ€๋ถ„์„ ๊ณ„์† this๋ผ๊ณ  ์จ์„œ ์—๋Ÿฌ๊ฐ€ ๋‚ฌ๋‹ค.

BaseUrl ์กฐ์‹ฌํ•˜๊ธฐ

object ReqresServiceImpl {
    private const val BASE_URL = "https://reqres.in/"
    //๋’ค์— ๋” ๋‚ด์šฉ
}

ReqreAdapter์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์˜ฌ๋•Œ, ๋‹ค์Œ๊ฐ์ฒด๊นŒ์ง€ ๋„˜๊ฒจ๋ฐ›์•„์™€์•ผํ•จ.

var data= mutableListOf<ReqresData.DataX>()

ReqresData๋งŒ ์ง‘์–ด๋„ฃ๊ณ , ๊ทธ ๋‹ค์Œ ๋‚ด๋ถ€ ์ฝ”๋“œ์—์„œ .data.dataX๋กœ ์“ฐ๋ ค๊ณ  ํ–ˆ๋”๋‹ˆ ์ž‘๋™ํ•˜์ง€ ์•Š์•˜๋‹ค.

holder.onBind(data[position])

์ด ๋ถ€๋ถ„๋„ ์ž‘๋™ํ•˜์ง€ ์•Š๊ฒŒ ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์œ„์ฒ˜๋Ÿผ ์ž‘์„ฑํ•ด์•ผํ•œ๋‹ค. ReqresData.DataX๋กœ

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages