笔记62 | 个人项目“易来”开发记录《一》实现号码测凶吉功能模块

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiangyong_1521/article/details/79074791

前言

快过春节,手里的项目都已经完结,空出大量的咸鱼时间,本着生命不止,折腾不息的学习热情,想做一个练手的东西,最好是能联网玩玩!于是“易来”就这么来了!


准备工作

  1. 聚合数据
    这个网站是一个提供API数据接口的网站,有付费和免费的申请,免费的只能申请一个接口,我登录我2年前申请的账号,发现已经申请了QQ号码测凶吉,回想应该是当时申请了,不知道怎么使用,就一直放在这。
  2. FastJson
    FastJson是阿里巴巴提供的一个开源Json处理工具包,除了知道它性能棒棒哒之外,一无所知!!!
  3. Volley
    我对这大兄弟较熟,每次想做加载图片的就会想到这哥们!

实现

搭界面这种社会主义初期阶段的代码就不好意思贴了,况且况且况且况且(是不是感觉有辆火车呼啸而来?得了,我就这么。。)况且(突然不知道况且是什么意思了)这界面简单!!
1. 拿到输入的数字:

numbtest_edit = (EditText) findViewById(R.id.numbtest_edit);
numbtest_edit.setFilters(new InputFilter[]{
new InputFilter.LengthFilter(10)
}); 
String numba = numbtest_edit.getText().toString();

我这里限制了可输入的字符长度,当拿到用户输入的信息后,通过点击测试按钮,就可以将number发送到聚合数据请求结果;

  1. 请求并接受数据:
private String urlq = "http://japi.juhe.cn/qqevaluate/qq?key=9eaeef7698c0d064*****e00a&qq=";
private void getString(final String ed) {
        String url=urlq+ed;//组合成请求的url
        StringRequest request = new StringRequest(url, 
                new Listener<String>() {
            @Override
            public void onResponse(String string) {
                try {
                    dealData(string,ed);//拿到结果
                } catch (Exception e) {

                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError arg0) {
            }
        });
        Volley.newRequestQueue(this).add(request);
    }

这个是volly的请求和接受数据方法,一气呵成,对于我们这种后排学生的好胜过了仓老师!

3.解析:

protected void dealData(String string,String ed)  {
        try {
            JSONObject jsonObject = new JSONObject(string);
            //Log.i("md","md: "+jsonObject.get("error_code")+":"+jsonObject.get("reason"));
            if(jsonObject.getInt("error_code")==0){//返回正常的信息
                JSONObject data = jsonObject.getJSONObject("result");  
                String currentLocation = data.getString("data"); 
                JSONObject jsonObject1 = new JSONObject(currentLocation);
                numbtest_result_info.setText(getText(R.string.numbtest_result_info3).toString()+ed+"\n"+"\n"+
                        getText(R.string.numbtest_result_info1).toString()+jsonObject1.optString("conclusion")+"\n"+"\n"+
                        getText(R.string.numbtest_result_info2).toString()+jsonObject1.optString("analysis"));
            }else{
                //返回错误提示
                numbtest_result_info.setText(getText(R.string.numbtest_result_info3).toString()+ed+"\n"+"\n"+
                        getText(R.string.numbtest_result_info1).toString()+ getText(R.string.numbtest_result_info4).toString()+"\n"+"\n"+
                        getText(R.string.numbtest_result_info2).toString()+jsonObject.get("reason"));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

因为数据简单,用import org.json.JSONObject官方自带的JSON解析就足够了,如果是很复杂的数据的话。。我就不玩了。。

  1. 细节添加:
    4.1 添加网络检测:
 <receiver android:name="com.evan.lift.utils.ConnectionChangeReceiver" >
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            </intent-filter>
        </receiver>

注册这个广播,当机器的网络发生变化时,就会收到广播,检测网络状态!

4.2 检测网络

private Timer mTimer = null;
    private TimerTask mTimerTask = null;
    /*
     * 全局定时器
     */
    private void onTime(final String s) {
        if (this.mTimerTask!=null) {
            this.mTimerTask.cancel();
        }
        if (this.mTimer==null) {
            this.mTimer=new Timer();
        }
        this.mTimerTask=new TimerTask() {

            @Override
            public void run() {
                        //
            }
        };
        this.mTimer.schedule(mTimerTask, 200, 2000);
    }

当网络发送变得时就启动一个定时器来监控网络连接状态:

netWorks = NetworkAvalible.isNetworkAvalible(getApplication());

4.3 还一些其他的UI信息,例如请求与接收到回执信息这个时间差里显示的网络连接信息可以根据请求与接受回执的方法中添加;
photo02.png

猜你喜欢

转载自blog.csdn.net/xiangyong_1521/article/details/79074791
62