Google官方网络框架Volley实战——QQ吉凶测试,南无阿弥陀佛!


这次我们用第三方的接口来做一个QQ吉凶的测试项目,代码依然是比较的简单

无图无真相

直接撸代码了,详细解释都已经写在注释里了

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/et_qq"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="@drawable/whitebg"
        android:gravity="center"
        android:hint="请输入QQ号"
        android:lines="3"
        android:numeric="integer" />

    <Button
        android:id="@+id/btn_go"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:layout_marginTop="5dp"
        android:background="@drawable/graybg"
        android:text="求佛" />

    <TextView
        android:id="@+id/tv_conclusion"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="5dp"
        android:text="结果"
        android:textSize="18sp" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#fff" />

    <TextView
        android:id="@+id/tv_analysis"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:layout_marginTop="5dp"
        android:text="分析"
        android:textSize="18sp" />

    <com.lgl.qq.WaterRippleView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >
    </com.lgl.qq.WaterRippleView>

</LinearLayout>

MainActivity

package com.lgl.qq;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request.Method;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

public class MainActivity extends Activity implements OnClickListener {

    private EditText et_qq;
    private Button btn_go;
    private TextView tv_conclusion, tv_analysis;

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

        initView();
    }

    private void initView() {
        // 初始化控件
        et_qq = (EditText) findViewById(R.id.et_qq);
        btn_go = (Button) findViewById(R.id.btn_go);
        btn_go.setOnClickListener(this);

        tv_conclusion = (TextView) findViewById(R.id.tv_conclusion);
        tv_analysis = (TextView) findViewById(R.id.tv_analysis);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btn_go:
            if (et_qq == null) {
                Toast.makeText(MainActivity.this, "都不留个QQ号佛主怎么算尼?",
                        Toast.LENGTH_LONG).show();
            } else {
                Volley_Get();
            }
            break;
        }
    }

    private void Volley_Get() {
        //获取到输入的QQ号
        String qq = et_qq.getText().toString();
        //第三方接口
        String url = "http://japi.juhe.cn/qqevaluate/qq?key=8d9160d4a96f2a6b5316de5b9d14d09d&qq="
                + qq;

        RequestQueue queue = Volley.newRequestQueue(this);
        StringRequest request = new StringRequest(Method.GET, url,
                new Listener<String>() {
                    // 成功
                    @Override
                    public void onResponse(String json) {
                        //Volley解析得到json
                        Volley_Json(json);
                    }
                }, new Response.ErrorListener() {
                    // 失败
                    @Override
                    public void onErrorResponse(VolleyError errorLog) {
                        Toast.makeText(MainActivity.this,
                                "失败:" + errorLog.toString(), Toast.LENGTH_LONG)
                                .show();
                    }
                });
        queue.add(request);
    }

    //解析json
    private void Volley_Json(String json) {
        try {
            //获得JSONObject对象
            JSONObject jsonObject = new JSONObject(json);
            //解析result
            JSONObject object = jsonObject.getJSONObject("result");
            //解析data
            JSONObject object1 = object.getJSONObject("data");
            tv_conclusion.setText("结果:" + object1.getString("conclusion"));
            tv_analysis.setText("分析:" + object1.getString("analysis"));
        } catch (JSONException e) {
            Toast.makeText(MainActivity.this, "施主都不留个QQ号佛主怎么算尼?",
                    Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }

    }
}

这里有几点需要说明

1.项目中的水波纹特效请看:Android特效专辑(一)——水波纹过渡特效(首页)

2.项目中的Button样式:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="#ffDEDEDE" />
    <corners android:radius="2.0dp" />
</shape>

3.项目中的EditText样式

<?xml version="1.0" encoding="utf-8"?>
<shape
  xmlns:android="http://schemas.android.com/apk/res/android">
     <solid android:color="#ffffffff"/>
   <corners android:radius="2.0dp"/>
</shape>

Demo下载:http://download.csdn.net/detail/qq_26787115/9397673

Google官方网络框架Volley实战——QQ吉凶测试,南无阿弥陀佛!的更多相关文章

  1. Google官方网络框架-Volley的使用解析Json以及加载网络图片方法

    Google官方网络框架-Volley的使用解析Json以及加载网络图片方法 Volley是什么? Google I/O 大会上,Google 推出 Volley的一个网络框架 Volley适合什么场 ...

  2. Android网络框架Volley(实战篇)

      之前讲了ym—— Android网络框架Volley(体验篇),大家应该了解了volley的使用,接下来我们要看看如何把volley使用到实战项目里面,我们先考虑下一些问题: 从上一篇来看 mQu ...

  3. Android网络框架Volley(体验篇)

    Volley是Google I/O 2013推出的网络通信库,在volley推出之前我们一般会选择比较成熟的第三方网络通信库,如: android-async-http retrofit okhttp ...

  4. ym—— Android网络框架Volley(终极篇)

    转载请注明本文出自Cym的博客(http://blog.csdn.net/cym492224103).谢谢支持! 没看使用过Volley的同学能够,先看看Android网络框架Volley(体验篇)和 ...

  5. Android网络框架Volley

    Volley是Google I/O 2013推出的网络通信库,在volley推出之前我们一般会选择比较成熟的第三方网络通信库,如: android-async-http retrofit okhttp ...

  6. GJM : Unity3D 常用网络框架与实战解析 【笔记】

    Unity常用网络框架与实战解析 1.Http协议          Http协议                  存在TCP 之上 有时候 TLS\SSL 之上 默认端口80 https 默认端口 ...

  7. Android热门网络框架Volley详解[申明:来源于网络]

    Android热门网络框架Volley详解[申明:来源于网络] 地址:http://www.cnblogs.com/caobotao/p/5071658.html

  8. Android网络框架-Volley实践 使用Volley打造自己定义ListView

    这篇文章翻译自Ravi Tamada博客中的Android Custom ListView with Image and Text using Volley 终于效果 这个ListView呈现了一些影 ...

  9. Android笔记(六十二)网络框架volley

    什么是Volley 很多时候,我们的APP都需要用到网络技术,使用HTTP协议来发送接收数据,谷歌推出了一个网络框架——volley,该框架适合进行数据量不大,但通信频繁的网络操作. 它的优点: (1 ...

随机推荐

  1. [Python]多个装饰器合并

    django程序,需要写很多api,每个函数都需要几个装饰器,例如 @csrf_exempt @require_POST def foo(request): pass 既然那么多个方法都需要写2个装饰 ...

  2. 64位Linux下安装mysql-5.7.13-linux-glibc2.5-x86_64 || 转载:http://www.cnblogs.com/gaojupeng/p/5727069.html

    由于公司临时让将Oracle的数据移植到mysql上面,所以让我在公司服务器上面安装一下mysql.下面就是我的安装过程以及一些错误解决思路.其实对于不同版本安装大体都有差不多. 1. 从官网下载 m ...

  3. Linux2.6--Linus电梯

          内核为了处理来自IO层的请求,需要进行相应的优化,因为当请求很多时,且请求的块又都几种在一块,那么如果按照顺序处理这些请求无疑是很大的时间开销,所以,我们需要寻求方法来处理这种情况(当然, ...

  4. UNIX网络编程——基本TCP套接字编程

    一.基于TCP协议的网络程序 下图是基于TCP协议的客户端/服务器程序的一般流程: 服务器调用socket().bind().listen()完成初始化后,调用accept()阻塞等待,处于监听端口的 ...

  5. Linux:ssh_config快速访问服务器

    在当前用户的根目录下: cd ~/.ssh vi config 编辑config内容为下面: ForwardAgent yes Host 1 Hostname 192.168.1.1 User roo ...

  6. 《java入门第一季》之集合框架TreeSet存储元素自然排序以及图解

    这一篇对TreeSet做介绍,先看一个简单的例子: * TreeSet:能够对元素按照某种规则进行排序.  * 排序有两种方式  * A:自然排序: 从小到大排序  * B:比较器排序    Comp ...

  7. Oracle EBS R12文件系统结构(学习汇总网上资料)

    Oracle EBS R12在服务器端文件结构如下: 顶层目录下面分为 1)inst --–跟ebs整个实例(instance)相关的配置信息以及其他信息 2)  db   ---主要存储DB层的信息 ...

  8. 如何在Cocos2D游戏中实现A*寻路算法(五)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...

  9. Gradle笔记——关于Gradle 1.12

    到目前为止,Gradle已经出到2.1版本了,从1.12这个版本开始看,主要是因为我使用Gradle是Android开发所需要.公司里面是采用Android Studio来进行Android项目的开发 ...

  10. Hibernate3 Criteria对象详解

    1.序言 Hibernate框架是目前JavaEE软件开发的企业主流框架,学习Hibernate必然要掌握ORM(对象关系映射Object/Relation Mapping)的概念思想, Hibern ...