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 ...
随机推荐
- Servlet规范总结
Servlet接口 Servlet规范的核心接口即是Servlet接口,它是所有Servlet类必须实现的接口,在Java Servelt API中已经提供了两个抽象类方便开发者实现Servlet类, ...
- Java基本语法-----java数组(一维数组二维数组)
嘿嘿!你们懂的,又是图片,委屈大家了. java数组(一维数组二维数组) [正在看本人博客的这位童鞋,我看你气度不凡,谈吐间隐隐有王者之气,日后必有一番作为!下面有个"顶"字,你就 ...
- Dynamics CRM 插件注册时报Assembly must be registered in isolation的解决方法
在插件注册的时候经常会遇到"Assembly must be registered in isolation"的问题导致无法注册,之前经常会被同事或者朋友问到这个问题,遇到这个问题 ...
- 02_c3p0之c3p0-config.xml配置案例,操作c3p0的jdbcUtil工具类的编写
c3p0也是一个开源jdbc连接池,我们熟悉的Hibernate和Spring框架使用的都是该数据源. 这里获得数据源使用的方法是:ComboPooledDataSource 它提供的构造方法有 ...
- 1086. Tree Traversals Again (25)
题目如下: An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For e ...
- unix下各种包安装方法备忘
deb包 : sudo dpkg -i google-chrome-stable_amd64.deb
- (SQL Server)有关T-SQL的10个好习惯
转自 http://www.cnblogs.com/CareySon/archive/2012/10/11/2719598.html 1.在生产环境中不要出现Select * 这一点我想大家已经是比较 ...
- CMake设置FOLDER失败及解决
CMake可以设置FOLDER属性,用来分目录组织VC中的多个工程. FOLDER: Set the folder name. Use to organize targets in an IDE. T ...
- 轻松学习Asp.net中的控件
C/S 结构,即大家熟知的客户机和服务器结构.它是软件系统体系结构,通过它可以充分利用两端硬件环境的优势,将任务合理分配到Client端和Server端来实现,降低了系统的通讯开销.目前大多数应用软件 ...
- 现代控制理论习题解答与Matlab程序示例
现代控制理论习题解答与Matlab程序示例 现代控制理论 第三版 课后习题参考解答: http://download.csdn.net/detail/zhangrelay/9544934 下面给出部分 ...