Google官方网络框架Volley实战——QQ吉凶测试,南无阿弥陀佛!
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吉凶测试,南无阿弥陀佛!的更多相关文章
- Google官方网络框架-Volley的使用解析Json以及加载网络图片方法
Google官方网络框架-Volley的使用解析Json以及加载网络图片方法 Volley是什么? Google I/O 大会上,Google 推出 Volley的一个网络框架 Volley适合什么场 ...
- Android网络框架Volley(实战篇)
之前讲了ym—— Android网络框架Volley(体验篇),大家应该了解了volley的使用,接下来我们要看看如何把volley使用到实战项目里面,我们先考虑下一些问题: 从上一篇来看 mQu ...
- Android网络框架Volley(体验篇)
Volley是Google I/O 2013推出的网络通信库,在volley推出之前我们一般会选择比较成熟的第三方网络通信库,如: android-async-http retrofit okhttp ...
- ym—— Android网络框架Volley(终极篇)
转载请注明本文出自Cym的博客(http://blog.csdn.net/cym492224103).谢谢支持! 没看使用过Volley的同学能够,先看看Android网络框架Volley(体验篇)和 ...
- Android网络框架Volley
Volley是Google I/O 2013推出的网络通信库,在volley推出之前我们一般会选择比较成熟的第三方网络通信库,如: android-async-http retrofit okhttp ...
- GJM : Unity3D 常用网络框架与实战解析 【笔记】
Unity常用网络框架与实战解析 1.Http协议 Http协议 存在TCP 之上 有时候 TLS\SSL 之上 默认端口80 https 默认端口 ...
- Android热门网络框架Volley详解[申明:来源于网络]
Android热门网络框架Volley详解[申明:来源于网络] 地址:http://www.cnblogs.com/caobotao/p/5071658.html
- Android网络框架-Volley实践 使用Volley打造自己定义ListView
这篇文章翻译自Ravi Tamada博客中的Android Custom ListView with Image and Text using Volley 终于效果 这个ListView呈现了一些影 ...
- Android笔记(六十二)网络框架volley
什么是Volley 很多时候,我们的APP都需要用到网络技术,使用HTTP协议来发送接收数据,谷歌推出了一个网络框架——volley,该框架适合进行数据量不大,但通信频繁的网络操作. 它的优点: (1 ...
随机推荐
- Eclipse打jar包,资源文件的读取
最近的工作中需要将java程序打一个jar包,然后在Linux中供调用.程序中需要读取一个配置文件.遇到了三个问题.第一个是依赖的第三方Jar包打成Jar包后找不到:第二个问题是资源文件所在的文件夹打 ...
- FFmpeg源代码简单分析:avformat_open_input()
===================================================== FFmpeg的库函数源代码分析文章列表: [架构图] FFmpeg源代码结构图 - 解码 F ...
- C链栈实现
#include <stdlib.h> #include <stdio.h> #include"LinkStack.h" const int TRUE = ...
- 【移动开发】SharedPreferences的兼容版本
public class SharedPreferencesCompat { private static final String TAG = SharedPreferencesCompat.cla ...
- iOS9 中关闭ATS的方法
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) iOS9中增加了系统的安全性,你会发现默认情况下打开非http ...
- Objc生成搜索引擎查询字符串
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 拿baidu为例,百度的搜索url为: http://www. ...
- Centos7安装Tair及配置测试
系统环境 Centos7 64位 外网ip 182.254.145.66 内网ip 10.105.23.114 安装位置 /usr/local/tair Tair介绍 参见官网 安装 想了半天,我还是 ...
- Android简易实战教程--第五话《开发一键锁屏应用》
转载请注明出处:http://blog.csdn.net/qq_32059827/article/details/51860900 点击打开链接 Device Administration 对于这个应 ...
- JS滚动显示
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding= ...
- inline内联函数
demo //带参数的宏 #define MYFUNC(a, b) ((a) < (b) ? (a) : (b)) inline int myfunc(int a, int b) { retur ...