时隔多年还能用android接私活

大家好,我是脚丫先生 (o^^o)

说起,android,这是多么遥远的英文单词。

「第一行代码」 我想学过android开发的小伙伴都应该读过,可谓是入门神作。
在这里插入图片描述

看着郭霖大佬的大作,此刻,有那么带点陌生而又带一丝熟悉的味道。

思绪仿佛一下子拉到2015年,临近毕业,毫无一技之长的我,披星戴月,废寝忘食的啃

着大佬的第一行代码,一切都犹如昨日,恍然过来,又已经过了很多年了。

值得聊的是,最近我在海外的一个平台上,用荒废多年的android技术接了一个app股票信息查询小项目(demo)。

本着中国人不骗中国人的原则,我发挥了十层话术功力,用一层的android技术说服了海外朋友。

需求是这样的: 做一个股票查询的app。

之后要在这个app基础上学一些软件安全方面的知识。

CS 5320 Homework .  	 Due on Canvas

Learning Outcomes
: Familiar with Android studio

Write a JAVA program in Android studio  and create a apk file to query information on stocks(current price,or something related). Android studio. For example, Type the stock symbol: TSLA to display the current price of Tesla.


The interface looks like this (Just a reference):

在这里插入图片描述

Type the TSLA, which is the stock symbol of Tesla, and get the display.

Note: You can use the existing stock acquisition API from the Internet resources.

我定眼一看,这破烂的UI,我这一层android技术还是能肝一肝。

于是果断的谈起了价格,海外平台的项目,自然而然是有中介小姐姐的。

首先,需要问的是价格,中介小姐姐给的1000,我一看,这么简单!!!竟然如此…

我就不客气了。于是乎,我就在这基础上扯了扯,加个几百块问题不大嘛。

在这里插入图片描述
于是最终以1500的价格接了下来。

虽然是一个very小白的项目。我最终也就花了一个小时磨磨蹭蹭的翻看郭神的第一行代码完成。

这里简单做一个分享吧。(虽然很烂)

在这里插入图片描述

一、股票app的界面

欢迎界面,在原本的需求上是没有的,但是本着对海外朋友的友好态度,我决定还是免费给加一个
在这里插入图片描述
如此漂亮的欢迎界面图我都敢上,代码自然是随之而来,一个线性布局足矣。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#0000D43C">

    <TextView

        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Welcome to stock inquiry"
        android:textSize="30sp"
        android:gravity="center"
        />
</LinearLayout>

So easy的股票查询app界面,只有这么简单了,瞧,我还给美化了一下呢。

采用了CardView作为股票信息的显示核心信息。

在这里插入图片描述

二、股票app的核心代码

根据海外朋友的强烈要求,要点击inquriry按钮后,立即在CardView上显示股票的核心信息。

那必须要实现,其实这里主要是去寻找股票信息接口,因为海外朋友要的是美股的。

https://finnhub.io/

这是一个免费的股票信息获取接口,大家可以看看自己是否需要。

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tvTicker;
    private TextView tvOpenPrice;
    private TextView tvPre;
    private TextView tvHigh;
    private TextView tvLowPrice;
    private EditText editText = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn = findViewById(R.id.btn_operate);
        editText = findViewById(R.id.et_operate);
        //StockName
        tvTicker = findViewById(R.id.stockName);
        //open
        tvOpenPrice = findViewById(R.id.openPrice);
        //Pre
        tvPre = findViewById(R.id.Pre);
        //HighPrice
        tvHigh = findViewById(R.id.HighPrice);
        //LowPrice
        tvLowPrice = findViewById(R.id.lowPrice);
        btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_operate){
           String symbol = editText.getText().toString();
           sendRequest(symbol);
        }
    }

    private void sendRequest(final String symbol) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                    String url = "https://api.polygon.io/v2/aggs/ticker/" + symbol+"/prev?unadjusted=true&apiKey=O0PC_KMaP8J7lLHpQ7czo16BktUF16y8";
                    OkHttpUtils.get().url(url).build().execute(new StringCallback() {
                        @Override
                        public void onError(Request request, Exception e) {
                            Toast.makeText(MainActivity.this,"Failed to obtain stock information !",Toast.LENGTH_SHORT).show();
                        }
                        @Override
                        public void onResponse(String response) {
                            try {
                                parseData(response);
                            } catch (JSONException e) {
                                Toast.makeText(MainActivity.this,e.toString(),Toast.LENGTH_SHORT).show();
                                e.printStackTrace();
                            }
                        }
                    });
            }
        }).start();
    }

    private void parseData(String responseData) throws JSONException {

        JSONObject dataObject = new JSONObject(responseData);
        JSONArray results = dataObject.getJSONArray("results");
        for (int i = 0 ; i < results.length(); i++){
            JSONObject resultsData = results.getJSONObject(i);
            //Ticker
            String ticker = resultsData.getString("T");
            //tvOpenPrice
            String open = resultsData.getString("o");
            //tvPrec
            String Prec = resultsData.getString("c");
            //tvHight
            String highest = resultsData.getString("h");
            //tvlow
            String lowest = resultsData.getString("l");
            showReponse(ticker,open,Prec,highest,lowest);
        }
    }

    private void showReponse(final String ticker,final String open, final String Prec,final String highest,final String lowest) {

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                tvTicker.setText(ticker);
                tvOpenPrice.setText(open);
                tvPre.setText(Prec);
                tvHigh.setText(highest);
                tvLowPrice.setText(lowest);
            }
        });

    }
}

三、总结

我个人觉得,我们在接小项目的时候,要勇于的去忽悠,勇于的去拉扯,要利于自己。

要把一个简单的demo项目说出花儿一样的难度,不要害怕失去。
在这里插入图片描述

如果,一个小项目要花费掉你所有的休息时间,我认为这是不值得的。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>