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. mysql进阶(二十七)数据库索引原理

    mysql进阶(二十七)数据库索引原理 前言   本文主要是阐述MySQL索引机制,主要是说明存储引擎Innodb.   第一部分主要从数据结构及算法理论层面讨论MySQL数据库索引的数理基础.    ...

  2. lucene查询索引库、分页、过滤、排序、高亮

    2.查询索引库 插入测试数据 xx.xx. index. ArticleIndex @Test public void testCreateIndexBatch() throws Exception{ ...

  3. 1.Android中解析json程序代码

    Android程序解析json数据可以通过gson的方式,这种情况需要导入相应的jar包.测试代码如下: @Override protected void onCreate(Bundle savedI ...

  4. 利用cocos2d-x实现CandyCrushSaga消除功能

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2D开发网–Cocos2Dev.com,谢谢! 原文地址: http://www.cocos2dev.com/?p=455 昨天没事写了个三消玩玩.已 ...

  5. 最简单的基于libVLC的例子:最简单的基于libVLC的视频播放器(图形界面版)

    ===================================================== 最简单的基于libVLC的例子文章列表: 最简单的基于libVLC的例子:最简单的基于lib ...

  6. iOS中 如何将自己的框架更新到cocopods上 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博! 为了更方便的集成第三方框架有了cocopods 的, 当我们有了相对比较好的框架的时候如何更新到cocopods ...

  7. 微信公众平台开发者中心服务器配置Token验证失败问题

    微信发展如火如荼,没有哪家的企业营销能避开微信不谈的,那像我们这种给客户实施项目的多多少少会涉及微信端的开发,本文只要给从未做过微信开发的人做一些简单的演示,行家里手们可以呵呵一下该干嘛干嘛去了. 微 ...

  8. Sharepoint2013部署ADFS 报new-sptrustedIdentityTokenIssuer:the trust provider certificate already exist

    在做sharepoint2013的adfs部署时,根据MSDN的步骤到新建身份验证程序时,powershell中报"ADFS new-sptrustedIdentityTokenIssuer ...

  9. listview下拉刷新上拉加载扩展(三)-仿最新版美团外卖

    本篇是基于上篇listview下拉刷新上拉加载扩展(二)-仿美团外卖改造而来,主要调整了headview的布局,并加了两个背景动画,看似高大上,其实很简单: as源码地址:http://downloa ...

  10. MOAC中“MO:安全性配置文件“对于开发者

    1. 获取配置文件的值:应用开发员->配置文件->输入用户配置文件名,找到上面的名称,即可填入fnd_profile.VALUE()中. 2. MO:安全性配置文件有值的话,就代表启用了M ...