이번에는 network 을 살펴본다



// TODO (1) Fill in this method to build the proper Github query URL

1. buildUrl method를 완성시킨다.

utilities 라는 package를 새로 생성.  NetworkUtils 라는 class도 생성

AS-IS : 껍데기만 있음

    public static URL buildUrl(String githubSearchQuery) {
        // TODO (1) Fill in this method to build the proper Github query URL
        return null;
    } 

-> TO-BE : 동작

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;

import java.net.MalformedURLException;
import java.net.URL;

final static String GITHUB_BASE_URL = "https://api.github.com/search/repositories";

final static String PARAM_QUERY = "q";

final static String PARAM_SORT = "sort";

final static String sortBy = "stars";

    public static URL buildUrl(String githubSearchQuery) {
        // COMPLETED (1) Fill in this method to build the proper Github query URL
        Uri builtUri = Uri.parse(GITHUB_BASE_URL).buildUpon()
                .appendQueryParameter(PARAM_QUERY, githubSearchQuery)
                .appendQueryParameter(PARAM_SORT, sortBy)
                .build();

        URL url = null;
        try {
            url = new URL(builtUri.toString());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        return url;
    } 


// TODO (2) Create a method called makeGithubSearchQuery
// TODO (3) Within this method, build the URL with the text from the EditText and set the built URL to the TextView
MainActivity 에서 method 추가한 뒤에 내용을 넣는다.

사용자가 입력한 EditText의 내용으로 github에 조회할 전체경로를 TextView에 보여준다.

    private void makeGithubSearchQuery() {
        String githubQuery = mSearchBoxEditText.getText().toString();
        URL githubSearchUrl = NetworkUtils.buildUrl(githubQuery);
        mUrlDisplayTextView.setText(githubSearchUrl.toString());
    } 


// TODO (4) Remove the Toast message when the search menu item is clicked
// TODO (5) Call makeGithubSearchQuery when the search menu item is clicked

클릭버튼 동작유무를 확인하기 위한 ToastMessage를 제거하고

makeGithubSearchQuery를 호출한다.

@Override
public boolean onOptionsItemSelected(MenuItem item){
    int itemThatWasClickedId = item.getItemId();

    if (itemThatWasClickedId == R.id.action_search) {
        // COMPLETED (4) Remove the Toast message when... is clicked
//            Context context = MainActivity.this;
//            String textToShow = "Search clicked";
//            Toast.makeText(context, textToShow, Toast.LENGTH_SHORT).show();


        // COMPLETED (5) Call makeGithubSearchQuery when... clicked
        makeGithubSearchQuery();

        return true;
    } 


앱을 실행시켜보면

queen이라는 단어 입력 후 Search버큰을 클릭하면

github로 날릴 조회쿼리가 표시된다.

아직 실제로 쿼리를 날린것이 아니므로 결과는 안나온다




728x90
BLOG main image
"그게 뭐 어쨌다는 거냐?" 늘 누가 나에게 나에대한 말을할 때면 이말을 기억해라. by nobang

카테고리

nobang이야기 (1933)
Life With Gopro (7)
Life With Mini (79)
Diary (971)
너 그거 아니(do you know) (162)
난 그래 (159)
Study (290)
속지말자 (10)
Project (34)
Poem (15)
Song (0)
Photo (113)
낙서장 (45)
일정 (0)
C.A.P.i (2)
PodCast (0)
nobang (27)
고한친구들 (4)
recieve (0)
History (0)
android_app (2)

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백

Total :
Today : Yesterday :