상세 컨텐츠

본문 제목

[안드로이드]업비트 api를 사용해서 현재가 정보 가져오기

안드로이드

by aries574 2021. 5. 28. 23:26

본문


2020.11.03 - [안드로이드] - [안드로이드]빗썸 api를 사용해서 현재가 정보 가져오기

 

[안드로이드]빗썸 api를 사용해서 현재가 정보 가져오기

빗썸거래소에서는 외부 개발자 및 사용자들이  쉽게 이용하여 서비스 및 애플리케이션을 개발할 수 있도록 api를 제공합니다. 이번 시간에는 해당 코인을 조회해서 현재가 정보를 가져오는 어

aries574.tistory.com

2020.11.04 - [안드로이드] - [안드로이드]빗썸 api를 사용해서 현재 매도/매수 정보 가져오기

 

[안드로이드]빗썸 api를 사용해서 현재 매도/매수 정보 가져오기

2020/11/03 - [안드로이드] - [안드로이드]빗썸 api를 사용해서 현재가 정보 가져오기 이전에 빗썸 api를 사용해서 현재가 정보를 가져오는 방법에 대해 썼습니다. 이번 시간에는 아래 그림과 같은 현

aries574.tistory.com

2020.11.06 - [안드로이드] - [안드로이드]빗썸 api 사용해서 해당 코인 체결내역 가져오기

 

[안드로이드]빗썸 api 사용해서 해당 코인 체결내역 가져오기

2020/11/03 - [안드로이드] - [안드로이드]빗썸 api를 사용해서 현재가 정보 가져오기 2020/11/04 - [안드로이드] - [안드로이드]빗썸 api를 사용해서 현재 매도/매수 정보 가져오기 이번시간에는 해당 코

aries574.tistory.com

 

이번 시간에는 업비트 거래소에서 사용하는 api를 사용해서 현재가 정보를 가져오는 간단한 앱을 만들어 보겠습니다.

1. 환경설정

AndroidManifest.xml

인터넷 권한을 추가해야 합니다.

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

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.kwon.upbitcoin">

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

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.kwon.upbitcoin">

     <uses-permission android:name="android.permission.INTERNET"/>
     <application
          android:allowBackup="true"
          android:icon="@mipmap/ic_launcher"
          android:label="@string/app_name"
          android:roundIcon="@mipmap/ic_launcher_round"
          android:supportsRtl="true"
          android:theme="@style/Theme.UpbitCoin">

          <activity android:name=".MainActivity">
               <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
               </intent-filter>
          </activity>
     </application>
</manifest>

 

 

build.gradle(Module: 앱이름.app)

dependencies 안에 라이브러리 추가

implementation 'com.android.volley:volley:1.2.0'
implementation 'org.json:json:20210307'

2020.12.19 - [안드로이드] - [안드로이드]라이브러리 찾아 등록하는 방법

 

[안드로이드]라이브러리 찾아 등록하는 방법

2020/12/18 - [안드로이드] - [안드로이드]TextSwitcher 글자변환 애니메이션 만들어보기 이번 시간에는 안드로이드 개발을 하면서 원하는 기능을 쓰기 위해 라이브러리를 찾아서 등록하는 방법을 알아

aries574.tistory.com

2. 화면구성(activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<EditText
android:id="@+id/edit_coinNm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="코인을 입력하세요" />

<Button
android:id="@+id/btn_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="조회" />


<!-- 시작가 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="시작가:"
android:textSize="15sp" />

<TextView
android:id="@+id/opening_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

<!-- 고가 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="고가:"
android:textSize="15sp" />

<TextView
android:id="@+id/high_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

<!-- 저가 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="저가:"
android:textSize="15sp" />

<TextView
android:id="@+id/low_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

<!-- 종가 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="종가:"
android:textSize="15sp" />

<TextView
android:id="@+id/trade_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

<!-- 전일 종가 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="전일종가:"
android:textSize="15sp" />

<TextView
android:id="@+id/prev_closing_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

<!-- 24시간 누적 거래대금 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="24시간 누적 거래대금:"
android:textSize="15sp" />

<TextView
android:id="@+id/acc_trade_price_24h"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>


<!-- 24시간 누적 거래량 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="24시간 누적 거래량:"
android:textSize="15sp" />

<TextView
android:id="@+id/acc_trade_volume_24h"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

</LinearLayout>

 

3. 기능구현(MainActivity.java)

public class MainActivity extends AppCompatActivity {

private RequestQueue requestQueue;

private static final String TAG = "Main";

EditText edit_coinNm;
TextView opening_price; //시가
TextView high_price; //고가
TextView low_price; //저가
TextView trade_price; //종가
TextView prev_closing_price;//전일 종가
TextView acc_trade_price_24h; // 24시간 누적 거래대금
TextView acc_trade_volume_24h;// 24시간 누적 거래량


@Override
protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);

     opening_price = findViewById(R.id.opening_price);
     high_price = findViewById(R.id.high_price);
     low_price = findViewById(R.id.low_price);
     trade_price = findViewById(R.id.trade_price);
     prev_closing_price = findViewById(R.id.prev_closing_price);
     acc_trade_price_24h = findViewById(R.id.acc_trade_price_24h);
     acc_trade_volume_24h = findViewById(R.id.acc_trade_volume_24h);

     Button btn_search = findViewById(R.id.btn_search);
     btn_search.setOnClickListener(new View.OnClickListener() {
          @Override
           public void onClick(View view) {

               edit_coinNm = findViewById(R.id.edit_coinNm);

               String coinNm = edit_coinNm.getText().toString().toUpperCase();

               if(!coinNm.isEmpty()){
                    getTicker(coinNm);
               }
           } //onClick
      }); //setOnClickListener

     if(requestQueue == null){
          requestQueue = Volley.newRequestQueue(getApplicationContext());
     }
  } //onCreate()

private void getTicker(String coinNm){

     final String coinName = coinNm;

     String url = "https://api.upbit.com/v1/ticker?markets=KRW-"+coinName;
     StringRequest request = new StringRequest(Request.Method.GET, url,
          new Response.Listener<String>() {
          @Override
          public void onResponse(String response) {
          //조회
               getTickerData(response);
          }
     },
          new Response.ErrorListener() {
          @Override
          public void onErrorResponse(VolleyError error) {
               Log.d(TAG, "onErrorResponse: " + error.getMessage());
          }
     }){
   };
   request.setShouldCache(false);
   requestQueue.add(request);
 }

private void getTickerData(String data) {

     try {
                JSONArray jsonArray = new JSONArray(data);

               for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);

                    String sPpening_price = jsonObject.get("opening_price").toString();//시가
                    String sHigh_price = jsonObject.get("high_price").toString();//고가
                    String sLow_price = jsonObject.get("low_price").toString();//저가
                    String sTrade_price = jsonObject.get("trade_price").toString();//종가
                    String sPrev_closing_price = jsonObject.get("prev_closing_price").toString();//전일종가
                    String sAcc_trade_price_24h = jsonObject.get("acc_trade_price_24h").toString();//24시간 누적거래대금
                   String sAcc_trade_volume_24h = jsonObject.get("acc_trade_volume_24h").toString();//24시간 누적거래량

                    //텍스트뷰에 데이터 담기
                    opening_price.setText(toDoubleFormat(Double.parseDouble(sPpening_price)));
                    high_price.setText(toDoubleFormat(Double.parseDouble(sHigh_price)));
                    low_price.setText(toDoubleFormat(Double.parseDouble(sLow_price)));
                    trade_price.setText(toDoubleFormat(Double.parseDouble(sTrade_price)));
                    prev_closing_price.setText(toDoubleFormat(Double.parseDouble(sPrev_closing_price)));
                    acc_trade_price_24h.setText(toDoubleFormat(Double.parseDouble(sAcc_trade_price_24h)));
                    acc_trade_volume_24h.setText(toDoubleFormat(Double.parseDouble(sAcc_trade_volume_24h)));
              }
        }catch (JSONException e){
            e.printStackTrace();
        }
} //getTickerData()

/**
* 1000자리 콤마
* @param num
* @return
*/
public String toDoubleFormat(Double num) {

     DecimalFormat df = null;

     if(num >= 100 && num <= 999.9){
          df = new DecimalFormat("000.0");
     }else if(num >= 10 && num <= 99.99){
          df = new DecimalFormat("00.00");
     }else if(num >= 1 && num <= 9.9999){
         df = new DecimalFormat("0.000");
     }else if(num < 1){
        df = new DecimalFormat("0.0000");
     }else{
       df = new DecimalFormat("###,###,###");
     }

     return df.format(num);
  }

 

 

 

 

2020.11.03 - [안드로이드] - [안드로이드]빗썸 api를 사용해서 현재가 정보 가져오기

 

[안드로이드]빗썸 api를 사용해서 현재가 정보 가져오기

빗썸거래소에서는 외부 개발자 및 사용자들이  쉽게 이용하여 서비스 및 애플리케이션을 개발할 수 있도록 api를 제공합니다. 이번 시간에는 해당 코인을 조회해서 현재가 정보를 가져오는 어

aries574.tistory.com

 

2020.11.04 - [안드로이드] - [안드로이드]빗썸 api를 사용해서 현재 매도/매수 정보 가져오기

 

[안드로이드]빗썸 api를 사용해서 현재 매도/매수 정보 가져오기

2020/11/03 - [안드로이드] - [안드로이드]빗썸 api를 사용해서 현재가 정보 가져오기 이전에 빗썸 api를 사용해서 현재가 정보를 가져오는 방법에 대해 썼습니다. 이번 시간에는 아래 그림과 같은 현

aries574.tistory.com

2020.11.06 - [안드로이드] - [안드로이드]빗썸 api 사용해서 해당 코인 체결내역 가져오기

 

[안드로이드]빗썸 api 사용해서 해당 코인 체결내역 가져오기

2020/11/03 - [안드로이드] - [안드로이드]빗썸 api를 사용해서 현재가 정보 가져오기 2020/11/04 - [안드로이드] - [안드로이드]빗썸 api를 사용해서 현재 매도/매수 정보 가져오기 이번시간에는 해당 코

aries574.tistory.com

 

맘에 드셨다면 공감부탁드려요

문의 댓글 환영합니다.

 

반응형

관련글 더보기

댓글 영역