Android

์–ดํ”Œ ๋งŒ๋“ค๊ธฐ : ํŽ˜์ด์ง•(์Šคํฌ๋กค) ์ฒ˜๋ฆฌ๋ฅผ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜๋Š” ์ฝ”๋“œ ์ž‘์„ฑํ•˜๊ธฐ

567Rabbit 2024. 6. 12. 17:14

https://codebunny99.tistory.com/164

 

์œ ํŠœ๋ธŒ API ํ‚ค์›Œ๋“œ ๊ฒ€์ƒ‰ํ•˜๋ฉด ํ‚ค์›Œ๋“œ์— ๋งž๋Š” ๊ฒŒ์‹œ๋ฌผ์„ ๊ฐ€์ ธ์˜ค๋Š” app๊ฐœ๋ฐœ

https://codebunny99.tistory.com/163 ์œ ํŠœ๋ธŒ ๋ฐ์ดํ„ฐ API๋ฅผ Postman(ํฌ์ŠคํŠธ๋งจ)์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•https://console.cloud.google.com/welcome/new?hl=ko&project=eastern-surface-426200-r0&supportedpurview=project Google ํด๋ผ์šฐ๋“œ ํ”Œ๋žซํผ๋กœ

codebunny99.tistory.com

 

์ด ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋ฒ ์ด์Šค๋กœ ํ•˜์—ฌ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์˜€๋‹ค.

 

 

 

 

MainActivity ์•ˆ์— ์ž‘์„ฑํ•œ๋‹ค

 

1. recyclerView ์Œ ๋ฐ‘ ์œ„์น˜์— //์Šคํฌ๋กค ์ฒ˜๋ฆฌ ~ ์ฝ”๋“œ ๋ฅผ์ž‘์„ฑํ•œ๋‹ค

 

 

 

// ์Šคํฌ๋กค ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ์ฝ”๋“œ
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
    @Override
    public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
    }

    @Override
    public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);

        int lastPosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findLastCompletelyVisibleItemPosition();
        int totalCount = recyclerView.getAdapter().getItemCount();

        if(lastPosition + 1 == totalCount){
            // ๋งจ ๋งˆ์ง€๋ง‰ ๋ฐ์ดํ„ฐ๊ฐ€ ํ™”๋ฉด์— ๋ณด์ด๊ฒŒ ๋˜๋ฉด
            // ๋„คํŠธ์›Œํฌ ํ†ตํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€๋กœ ๊ฐ€์ ธ์˜ค๋„๋ก ํ•œ๋‹ค.
            addNetworkData();
        }
    }
});

 

 

 

 

 

2. ํฌ์ŠคํŠธ๋งจ์—์„œ nextPageToken์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•œ๋‹ค.

 

 

 

 

 

3. MainActivity์—์„œ nextPageToken ํŒŒ์‹ฑ ๋ถ€๋ถ„์„ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค.

 

 

๋ฉค๋ฒ„๋ณ€์ˆ˜์— ์ถ”๊ฐ€ํ•˜๊ธฐ

 

 

 

nextPageToken์„ ๊ธฐ์กด์˜ ํŒŒ์‹ฑ์— ์ถ”๊ฐ€ํ•ด ํŒŒ์‹ฑํ•ด์ค€๋‹ค.

 

 

 

 

4. MainActivity์— addNetworkData() ํ•จ์ˆ˜ ๋งŒ๋“ค์–ด์ฃผ๊ธฐ(Create)

 

 

 

๋ˆ„๋ฅด๋ฉด, ์•„๋ž˜์™€ ๊ฐ™์€ ์œ„์น˜์— ํ•จ์ˆ˜๊ฐ€ ๋งŒ๋“ค์–ด์ง€๋Š”๋ฐ, ์•„๋ž˜์™€ ๊ฐ™์ด nextPageToken ๋ถ€๋ถ„์„ ์ถ”๊ฐ€๋กœ ์ž‘์„ฑํ•ด์ค€๋‹ค

 

// ์ถ”๊ฐ€๋กœ 20๊ฐœ์”ฉ์„ ๋” ํ˜ธ์ถœํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜
    private void addNetworkData() {

        progressBar.setVisibility(View.VISIBLE);

        // ์œ ํŠœ๋ธŒ API๋ฅผ ํ˜ธ์ถœํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์˜จ๋‹ค
        RequestQueue queue = Volley.newRequestQueue(MainActivity.this);

        String url = "https://www.googleapis.com/youtube/v3/search?key="
                + Config.YoutubeKey
                + "&part=snippet&maxResults=20&type=video&order=date&q="
                + keyword
                + "&pageToken=" + nextPageToken;

 

 

 

 

์•„๋ž˜๋ถ€ํ„ฐ๋Š” ๊ธฐ์กด์— ์ƒ์„ฑํ•œ onCreate ํ•จ์ˆ˜์—์„œ ๋ณต์‚ฌํ•˜์—ฌ ๊ทธ๋Œ€๋กœ ๋ถ™์—ฌ๋„ฃ๊ธฐ ํ•œ ํ›„์— url์„ ์œ„์— ์ƒ์„ฑํ•˜์˜€๊ธฐ ๋•Œ๋ฌธ์—

 

url์ด๋ผ๊ณ  ๋ฐ”๊ฟ”์ค€ ๋’ค์—,

        JsonObjectRequest request = new JsonObjectRequest(
                Request.Method.GET,
                url,
                null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {

                        // ํ”„๋กœ๊ทธ๋ ˆ์Šค ๋ฐ”๋ฅผ ์œ ์ €์˜ ๋ˆˆ์—์„œ ์‚ฌ๋ผ์ง€๊ฒŒ ํ•˜๋Š” ์ฝ”๋“œ
                        progressBar.setVisibility(View.GONE);

                        try {
                            nextPageToken = response.getString("nextPageToken");

                            JSONArray items = response.getJSONArray("items");

                            for (int i = 0; i < items.length(); i++) {
                                JSONObject data = items.getJSONObject(i);
                                JSONObject id = data.getJSONObject("id");
                                String videoId = id.getString("videoId");
                                JSONObject snippet = data.getJSONObject("snippet");
                                String title = snippet.getString("title");
                                String description = snippet.getString("description");

                                JSONObject thumbnails = snippet.getJSONObject("thumbnails");
                                JSONObject medium = thumbnails.getJSONObject("medium");
                                String thumbUrl = medium.getString("url");

                                JSONObject high = thumbnails.getJSONObject("high");
                                String url = high.getString("url");

                                Youtube youtube = new Youtube(videoId, title, description, thumbUrl, url);
                                YoutubeArrayList.add(youtube);
                            }

                            adapter.notifyDataSetChanged();

                        } catch (JSONException e) {
                            Toast.makeText(MainActivity.this, "ํŒŒ์‹ฑ ์—๋Ÿฌ", Toast.LENGTH_SHORT).show();
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        // ํ”„๋กœ๊ทธ๋ ˆ์Šค ๋ฐ”๋ฅผ ์œ ์ €์˜ ๋ˆˆ์—์„œ ์‚ฌ๋ผ์ง€๊ฒŒ ํ•˜๋Š” ์ฝ”๋“œ
                        progressBar.setVisibility(View.GONE);

                        Toast.makeText(MainActivity.this, "๋„คํŠธ์›Œํฌ ํ†ต์‹  ์—๋Ÿฌ", Toast.LENGTH_SHORT).show();
                    }
                }
        );

        queue.add(request);

 

 

 

์ค‘๊ฐ„์— //์‚ญ์ œํ•  ๋ถ€๋ถ„์€ ์‚ญ์ œํ•ด์ฃผ๊ณ , //์ถ”๊ฐ€ํ•  ๋ถ€๋ถ„์€ ์‚ญ์ œํ•œ ๊ทธ ์ž๋ฆฌ์— ์ƒˆ๋กœ์šด ์–ด๋Œ‘ํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

// ์‚ญ์ œํ•  ๋ถ€๋ถ„
YoutubeArrayList.clear();  // ๊ธฐ์กด ๋ฐ์ดํ„ฐ ์ดˆ๊ธฐํ™”

adapter = new YoutubeAdapter(MainActivity.this, YoutubeArrayList);
recyclerView.setAdapter(adapter);

// ์ถ”๊ฐ€ํ•  ๋ถ€๋ถ„
adapter.notifyDataSetChanged();

 

 

 

 

+

<Retrofit2 ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ>

 

4๋ฒˆ ๋Œ€์‹ ์— MainActivity์— ์ด ๋ถ€๋ถ„์„ ์ž‘์„ฑํ•ด์ค€๋‹ค.

private void addNetworkData() {

        progressBar.setVisibility(View.VISIBLE);

        Retrofit retrofit = NetworkClient.getRetrofitClient(MainActivity.this);

        PostingApi api = retrofit.create(PostingApi.class);

        Call<PostingList> call = api.getPostingList("Bearer " + token, offset, limit );

        call.enqueue(new Callback<PostingList>() {
            @Override
            public void onResponse(Call<PostingList> call, Response<PostingList> response) {
                progressBar.setVisibility(View.GONE);

                if(response.isSuccessful()){
                    PostingList postingList = response.body();

                    count = postingList.count;

                    offset = offset + count;

                    postingArrayList.addAll( postingList.items );

                    adapter.notifyDataSetChanged();

                }else{

                }
            }