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. 错误问题:OpenGL version to old,GLViewinitWithRect(const stdbasic_stringchar,stdchar_traitschar,stdalloca

     1电脑装成Linux之后,在Linux里面装虚拟机运行cocos2d-x-3.2时报如下错误: plan3d.exe!cocos2d::GLView::initWithRect(const st ...

  2. Socket实现单客户端与服务器对话功能

    单客户端,顾名思义,就是客户端只有一个用户去访问服务器,然后服务器根据该客户请求返回信息,先看下效果图: 服务端(左)和客户端(右): 注意,我是用了两个eclipse,一个只放服务端文件,一个只放客 ...

  3. FFmpeg的H.264解码器源代码简单分析:概述

    ===================================================== H.264源代码分析文章列表: [编码 - x264] x264源代码简单分析:概述 x26 ...

  4. iOS7 CookBook精彩瞬间(三)UIActivityViewController的基本使用及自定义Activity

    1.基本使用 UIActivityViewController主要用于分享内容,创建activityView的方法很简单,调用下面的方法创建: [[UIActivityViewController a ...

  5. 【iOS 开发】基本 UI 控件详解 (UIButton | UITextField | UITextView | UISwitch)

    博客地址 : http://blog.csdn.net/shulianghan/article/details/50051499 ; 一. UI 控件简介 1. UI 控件分类 UI 控件分类 : 活 ...

  6. 【Netty源码学习】入门示例

    Netty是由JBOSS提供的一个java开源框架.Netty提供异步的.事件驱动的网络应用程序框架和工具,用以快速开发高性能.高可靠性的网络服务器和客户端程序.        也就是说,Netty ...

  7. Hive drop table卡住的问题

    在hive中,show tables,create 等命令能正常执行,删除表drop table x时,会出现卡住的现象. 进入mysql, show variables like 'char%' 可 ...

  8. log4j 日志限制大小 拆分成30个 不按日期分日志 按大小拆分 按日期产生

    先说一下按日期产生,不解释,大家都懂,这种方法的缺点就是很吃硬盘空间 log4j.rootLogger=INFO,logfile,stdout log4j.logger.java.sql=DEBUG, ...

  9. 纯HTML5APP与原生APP的差距在哪?

    笔者写过一些纯H5的APP,虽然开发起来的确很快很舒服,但和原生比起来纯H5APP还是有很多问题,主要聚集在以下几个方面: 1.动画 动画有很多种,比如侧边栏菜单的滑入滑出.元素的响应动画.页面切换之 ...

  10. ios swift 实现饼状图进度条,swift环形进度条

    ios swift 实现饼状图进度条 // // ProgressControl.swift // L02MyProgressControl // // Created by plter on 7/2 ...