一、说在前面

  昨天

学习了序列化的相关知识

  今天

1、学习 volley(HTTP库)的 StringRequest请求

2、使用序列化完成相关案例

遇到问题

请求到的参数的出现中文乱码问题

问题的解决:自定义StringRequest类 修改volley编码为utf-8, 默认为Latin1 中文显示乱码。

package com.me.myvolley;

import androidx.annotation.Nullable;

import com.android.volley.NetworkResponse;
import com.android.volley.Response;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest; import java.io.UnsupportedEncodingException; public class Utf8StringRequest extends StringRequest { public Utf8StringRequest(int method, String url, Response.Listener<String> listener, @Nullable Response.ErrorListener errorListener) {
super(method, url, listener, errorListener);
} @Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
String parsed = "";
try {
parsed = new String(response.data,"utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response));
}
}

二、volley 简介

1、特点

  1)Volley是一个HTTP库,它使Android应用程序的网络更容易,最重要的是,更快,适合高并发的网络请求。网络请求 cancel 机制。我们可以取消单个请求,或者指定取消请求队列中的一个区域;自动调度网络请求;
  2)Volley不适合大型下载或流式操作,因为Volley在解析期间将所有响应保存在内存中。对于大型下载操作,请考虑使用类似的替代方法DownloadManager。

2、组成

1)网络请求(StringRequest,JsonArrayRequest,JsonObjectRequest,ImageRequest)。
2)图片加载 ImageLoader
3)自定义ImageView NetworkImageView
 

三、案例:获取邯郸市当天的天气信息

1、设计思路

1)api接口介绍:https://api.help.bj.cn/api/?id=45
2)根据api接口和实际需求创建相应的天气实体类。
3)根据天气实体类创建相应的UI界面。
4)创建请求队列,创建StringRequest请求,将请求加入到队列中。
5)将从网上获取的数据(Json格式)使用Gson转换为java对象。
6)将数据与UI界面的组件绑定。

2、代码:

1)api获取的数据格式及:
{
"status": "0", //反馈代码 0成功
"msg": "反馈信息", //反馈信息
"cityen": "changchun", //城市名称英文
"city": "长春", //城市名称
"citycode": "101060101", //城市编码
"temp": "10", //实时温度
"tempf": "50", //华氏温度
"wd": "西风", //风向
"wden": "W", //风向英文
"wdforce": "3级", //风力
"wdspd": "<12km/h", //风速
"uptime": "12:00", //更新时间
"weather": "晴", //天气状况
"weatheren": "Sunny", //天气状况英文
"weatherimg": "d00", //天气状况图标
"stp": "994", //气压
"wisib": "35000", //能见度
"humidity": "46%", //湿度
"prcp": "0", //降雨
"prcp24h": "2.2", //24小时降雨量
"aqi": "22", //AQI
"pm25": "20", //PM2.5
"today": "10月17日(星期一)" //今天日期
}

2)天气实体类

package com.me.myvolley;

public class TianQi {
private String city;
private String today;
private String weather;
private int pm25;
private int temp;
private String wd;
private String wdforce; public String getCity() {
return city;
} public void setCity(String city) {
this.city = city;
} public String getToday() {
return today;
} public void setToday(String today) {
this.today = today;
} public String getWeather() {
return weather;
} public void setWeather(String weather) {
this.weather = weather;
} public int getPm25() {
return pm25;
} public void setPm25(int pm25) {
this.pm25 = pm25;
} public int getTemp() {
return temp;
} public void setTemp(int temp) {
this.temp = temp;
} public String getWd() {
return wd;
} public void setWd(String wd) {
this.wd = wd;
} public String getWdforce() {
return wdforce;
} public void setWdforce(String wdforce) {
this.wdforce = wdforce;
} }
3)UI界面。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"> <TextView
android:id="@+id/textViewWdforce"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="36sp"
app:layout_constraintBottom_toTopOf="@+id/guideline"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textViewWd"
tools:text="tianqi" /> <TextView
android:id="@+id/textViewCity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/title_font_size"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.05"
tools:text="handan1111" /> <TextView
android:id="@+id/textViewDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="36sp"
app:layout_constraintBottom_toTopOf="@+id/textViewWeather"
app:layout_constraintEnd_toEndOf="@+id/textViewWeather"
app:layout_constraintStart_toStartOf="@+id/textViewWeather"
app:layout_constraintTop_toBottomOf="@+id/textViewCity"
tools:text="tianqi" /> <TextView
android:id="@+id/textViewWeather"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="36sp"
app:layout_constraintBottom_toTopOf="@+id/textViewPm25"
app:layout_constraintEnd_toEndOf="@+id/textViewPm25"
app:layout_constraintStart_toStartOf="@+id/textViewPm25"
app:layout_constraintTop_toBottomOf="@+id/textViewDate"
tools:text="tianqi" /> <TextView
android:id="@+id/textViewPm25"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="36sp"
app:layout_constraintBottom_toTopOf="@+id/textViewTemp"
app:layout_constraintEnd_toEndOf="@+id/textViewTemp"
app:layout_constraintStart_toStartOf="@+id/textViewTemp"
app:layout_constraintTop_toBottomOf="@+id/textViewWeather"
tools:text="tianqi" /> <TextView
android:id="@+id/textViewTemp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="36sp"
app:layout_constraintBottom_toTopOf="@+id/textViewWd"
app:layout_constraintEnd_toEndOf="@+id/textViewWd"
app:layout_constraintStart_toStartOf="@+id/textViewWd"
app:layout_constraintTop_toBottomOf="@+id/textViewPm25"
tools:text="tianqi" /> <TextView
android:id="@+id/textViewWd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="36sp"
app:layout_constraintBottom_toTopOf="@+id/textViewWdforce"
app:layout_constraintEnd_toEndOf="@+id/textViewWdforce"
app:layout_constraintStart_toStartOf="@+id/textViewWdforce"
app:layout_constraintTop_toBottomOf="@+id/textViewTemp"
tools:text="tianqi" /> <androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.9" />
</androidx.constraintlayout.widget.ConstraintLayout>
4)创建请求队列,创建StringRequest请求,将请求加入到队列中,将从网上获取的数据(Json格式)使用Gson转换为java对象,将数据与UI界面的组件绑定。
package com.me.myvolley;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView; import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson; public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//String url = "https://www.baidu.com";
String url = "https://api.help.bj.cn/apis/weather/?id=101091001";
final TextView textViewWeather = findViewById(R.id.textViewWeather);
final TextView textViewCity = findViewById(R.id.textViewCity);
final TextView textViewDate = findViewById(R.id.textViewDate);
final TextView textViewPm25 = findViewById(R.id.textViewPm25);
final TextView textViewTemp = findViewById(R.id.textViewTemp);
final TextView textViewWd = findViewById(R.id.textViewWd);
final TextView textViewWdforce = findViewById(R.id.textViewWdforce);
//1、创建一个队列
RequestQueue queue = Volley.newRequestQueue(this);
//2、创建一个request
final Utf8StringRequest request = new Utf8StringRequest(
StringRequest.Method.GET, //1、请求方式
url, //2、请求网址
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Gson gson = new Gson();
TianQi tianQi = gson.fromJson(response,TianQi.class);
textViewCity.setText(tianQi.getCity());
textViewDate.setText(tianQi.getToday());
textViewPm25.setText("PM2.5:" + String.valueOf(tianQi.getPm25()));
textViewTemp.setText("温度:" + String.valueOf(tianQi.getTemp()));
textViewWd.setText("风向:" + tianQi.getWd());
textViewWdforce.setText("风力:" + tianQi.getWdforce());
textViewWeather.setText("天气:" + tianQi.getWeather());
//First first = gson.fromJson(response,First.class);
//textView.setText(response);
}
}, //3、成功的回调函数
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
textViewCity.setText("请求错误!");
}
} //4、失败的回调函数
);
queue.add(request);
}
}

四、案例运行测试

2.15 学习总结 之 天气预报APP volley(HTTP库)之StringRequest的更多相关文章

  1. android入门学习-天气预报app(一)

    引言 学习<android第一行代码>根据书本开发的天气预报app,主要用于熟练操作android开发(android studio3.0平台). 今天主要分享一下从服务器上获取天气信息, ...

  2. 毕业设计--天气预报App

    9月中旬,开始动手做我的毕业设计了,之前一直在纠结做啥,后来想想,既然是做毕业设计,那就大胆地做点自己没接触过的东西吧.然后网上查找资料得知做天气预报需要用到开放的API,而且要用那种现在还在维护的, ...

  3. 用Swift实现一款天气预报APP(三)

    这个系列的目录: 用Swift实现一款天气预报APP(一) 用Swift实现一款天气预报APP(二) 用Swift实现一款天气预报APP(三) 通过前面的学习,一个天气预报的APP已经基本可用了.至少 ...

  4. 用Swift实现一款天气预报APP(二)

    这个系列的目录: 用Swift实现一款天气预报APP(一) 用Swift实现一款天气预报APP(二) 用Swift实现一款天气预报APP(三) 上篇中主要讲了界面的一些内容,这篇主要讨论网络请求,获得 ...

  5. 天气预报APP(1)

    一个天气预报APP至少应该具备以下功能: *可以罗列出全国所有的省.市.县: *可以查看全国任意城市的天气信息: *可以自由的切换城市,去查看其他城市的天气: *提供手动更新以及后台自动更新天气的功能 ...

  6. React Native指南汇集了各类react-native学习资源、开源App和组件

    来自:https://github.com/ele828/react-native-guide React Native指南汇集了各类react-native学习资源.开源App和组件 React-N ...

  7. 用Swift实现一款天气预报APP(一)

    这个系列的目录: 用Swift实现一款天气预报APP(一) 用Swift实现一款天气预报APP(二) 用Swift实现一款天气预报APP(三) Swift作为现在苹果极力推广的语言,发展的非常快.这个 ...

  8. 基于Android开发的天气预报app(源码下载)

    原文:基于Android开发的天气预报app(源码下载) 基于AndroidStudio环境开发的天气app -系统总体介绍:本天气app使用AndroidStudio这个IDE工具在Windows1 ...

  9. Python Cookbook(第3版)中文版:15.14 传递Unicode字符串给C函数库

    15.14 传递Unicode字符串给C函数库¶ 问题¶ 你要写一个扩展模块,需要将一个Python字符串传递给C的某个库函数,但是这个函数不知道该怎么处理Unicode. 解决方案¶ 这里我们需要考 ...

随机推荐

  1. LinkStack(链栈)

    链栈即链式栈,也就是说我们不用再考虑空间的大小,可随心所欲的进行数据的插入/删除了.和顺序栈一样,仍然要保持其stack的特性,只在一端进行插入和删除,后进先出. (2018-02-14 代码更新) ...

  2. 大数据篇:MapReduce

    MapReduce MapReduce是什么? MapReduce源自于Google发表于2004年12月的MapReduce论文,是面向大数据并行处理的计算模型.框架和平台,而Hadoop MapR ...

  3. 信息论相关概念:熵 交叉熵 KL散度 JS散度

    目录 机器学习基础--信息论相关概念总结以及理解 1. 信息量(熵) 2. KL散度 3. 交叉熵 4. JS散度 机器学习基础--信息论相关概念总结以及理解 摘要: 熵(entropy).KL 散度 ...

  4. Ngnix调整

    1.隐藏版本号,防止针对版本攻击   http {    server_tokens off;2.增加并发连接   2.1 worker_processes :改为CPU核数一致,因为异步IO进程是单 ...

  5. 杭电2014 (第一次用vector ac题目)

    早就想用容器类来实现一些编程,今天也算是学了一点吧. vector的使用方法参考了某位博主的一篇文章,感觉写得还是不错的:http://blog.csdn.net/always2015/article ...

  6. zookeeper和eureka的区别在哪?

    zookeeper和eureka的区别在哪?传统的关系型数据库是ACID(原子性,一致性,独立性,持久性), nosql数据库是CAP(强一致性,可用性,分区容错性),分布式系统只能3进2,三个选两个 ...

  7. C# String 字符串一些关键理解

    #1 :在.Net Framework中,字符总是表示成16位Unicode的代码#2 :String 和string 其实是一样的只是表现形式上不同#3 :string类型被视为基元类型,也就是编译 ...

  8. 「BJOI2018」求和

    「BJOI2018」求和 传送门 观察到 \(k\) 很小而且模数不会变,所以我们直接预处理 \(k\) 取所有值时树上前缀答案,查询的时候差分一下即可. 参考代码: #include <alg ...

  9. 题解 JSOI2010 找零钱的洁癖

    题解 JSOI2010 找零钱的洁癖 题面 BZOJ 个人体会 van全没有思路... 只能去看题解... 还是个bfs+贪心 不管怎样竟然乱搞过了... 听M_sea小姐姐说她有更正经的做法(线性规 ...

  10. 「Luogu4556」Vani有约会-雨天的尾巴

    「Luogu4556」Vani有约会-雨天的尾巴 传送门 很显然可以考虑树上差分+桶,每次更新一条链就是把这条链上的点在桶对应位置打上 \(1\) 的标记, 最后对每个点取桶中非零值的位置作为答案即可 ...