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

本文涉及的产品
云解析 DNS,旗舰版 1个月
全局流量管理 GTM,标准版 1个月
公共DNS(含HTTPDNS解析),每月1000万次HTTP解析
简介: <div class="markdown_views"><h1 id="google官方网络框架volley实战qq吉凶测试南无阿弥陀佛">Google官方网络框架Volley实战——QQ吉凶测试,南无阿弥陀佛!</h1><hr><blockquote> <p>这次我们用第三方的接口来做一个QQ吉凶的测试项目,代码依然是比较的简单 </p></blockquote

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

目录
相关文章
|
8天前
|
机器学习/深度学习 API Python
Python 高级编程与实战:深入理解网络编程与异步IO
在前几篇文章中,我们探讨了 Python 的基础语法、面向对象编程、函数式编程、元编程、性能优化、调试技巧、数据科学、机器学习、Web 开发和 API 设计。本文将深入探讨 Python 在网络编程和异步IO中的应用,并通过实战项目帮助你掌握这些技术。
|
6天前
|
监控 安全 Cloud Native
企业网络架构安全持续增强框架
企业网络架构安全评估与防护体系构建需采用分层防御、动态适应、主动治理的方法。通过系统化的实施框架,涵盖分层安全架构(核心、基础、边界、终端、治理层)和动态安全能力集成(持续监控、自动化响应、自适应防护)。关键步骤包括系统性风险评估、零信任网络重构、纵深防御技术选型及云原生安全集成。最终形成韧性安全架构,实现从被动防御到主动免疫的转变,确保安全投入与业务创新的平衡。
|
3月前
|
机器学习/深度学习 算法 PyTorch
基于图神经网络的大语言模型检索增强生成框架研究:面向知识图谱推理的优化与扩展
本文探讨了图神经网络(GNN)与大型语言模型(LLM)结合在知识图谱问答中的应用。研究首先基于G-Retriever构建了探索性模型,然后深入分析了GNN-RAG架构,通过敏感性研究和架构改进,显著提升了模型的推理能力和答案质量。实验结果表明,改进后的模型在多个评估指标上取得了显著提升,特别是在精确率和召回率方面。最后,文章提出了反思机制和教师网络的概念,进一步增强了模型的推理能力。
118 4
基于图神经网络的大语言模型检索增强生成框架研究:面向知识图谱推理的优化与扩展
|
4月前
|
人工智能 自然语言处理
WebDreamer:基于大语言模型模拟网页交互增强网络规划能力的框架
WebDreamer是一个基于大型语言模型(LLMs)的网络智能体框架,通过模拟网页交互来增强网络规划能力。它利用GPT-4o作为世界模型,预测用户行为及其结果,优化决策过程,提高性能和安全性。WebDreamer的核心在于“做梦”概念,即在实际采取行动前,用LLM预测每个可能步骤的结果,并选择最有可能实现目标的行动。
107 1
WebDreamer:基于大语言模型模拟网页交互增强网络规划能力的框架
|
3月前
|
存储 安全 网络安全
网络安全的盾与剑:漏洞防御与加密技术的实战应用
在数字化浪潮中,网络安全成为保护信息资产的重中之重。本文将深入探讨网络安全的两个关键领域——安全漏洞的防御策略和加密技术的应用,通过具体案例分析常见的安全威胁,并提供实用的防护措施。同时,我们将展示如何利用Python编程语言实现简单的加密算法,增强读者的安全意识和技术能力。文章旨在为非专业读者提供一扇了解网络安全复杂世界的窗口,以及为专业人士提供可立即投入使用的技术参考。
|
3月前
|
存储 缓存 监控
Docker容器性能调优的关键技巧,涵盖CPU、内存、网络及磁盘I/O的优化策略,结合实战案例,旨在帮助读者有效提升Docker容器的性能与稳定性。
本文介绍了Docker容器性能调优的关键技巧,涵盖CPU、内存、网络及磁盘I/O的优化策略,结合实战案例,旨在帮助读者有效提升Docker容器的性能与稳定性。
299 7
|
4月前
|
人工智能 测试技术
Google Gemini意外超越OpenAI,跃居第一,但基准测试结果并不能说明全部情况
Google Gemini意外超越OpenAI,跃居第一,但基准测试结果并不能说明全部情况
|
4月前
|
JSON 数据处理 Swift
Swift 中的网络编程,主要介绍了 URLSession 和 Alamofire 两大框架的特点、用法及实际应用
本文深入探讨了 Swift 中的网络编程,主要介绍了 URLSession 和 Alamofire 两大框架的特点、用法及实际应用。URLSession 由苹果提供,支持底层网络控制;Alamofire 则是在 URLSession 基础上增加了更简洁的接口和功能扩展。文章通过具体案例对比了两者的使用方法,帮助开发者根据需求选择合适的网络编程工具。
73 3
|
4月前
|
数据库连接 Go 数据库
Go语言中的错误注入与防御编程。错误注入通过模拟网络故障、数据库错误等,测试系统稳定性
本文探讨了Go语言中的错误注入与防御编程。错误注入通过模拟网络故障、数据库错误等,测试系统稳定性;防御编程则强调在编码时考虑各种错误情况,确保程序健壮性。文章详细介绍了这两种技术在Go语言中的实现方法及其重要性,旨在提升软件质量和可靠性。
74 1
|
4月前
|
网络协议 Unix Linux
精选2款C#/.NET开源且功能强大的网络通信框架
精选2款C#/.NET开源且功能强大的网络通信框架
132 0

热门文章

最新文章