初识Volley框架 加载以及查看json数据+html元素
来源:互联网 发布:淘宝旺铺图片轮播尺寸 编辑:程序博客网 时间:2024/06/05 18:40
本文主要讲解使用框架Volley进行html元素的查看以及json数据的查看。
例子较为简单,这里博主就不做过多陈述。直接给大家上源码:
activity_volley_using.xml
<?xml version="1.0" encoding="utf-8"?><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" tools:context="com.ynu.demoforvolley.VolleyUsing"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/request" android:text="网址解析" android:onClick="Request"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/request1" android:text="Json解析" android:onClick="Request1"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/request2" android:text="Gson请求并展示" android:onClick="Request2"/></LinearLayout>VolleyUsing.java
import android.content.Context;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import com.android.volley.RequestQueue;import com.android.volley.Response;import com.android.volley.VolleyError;import com.android.volley.toolbox.JsonObjectRequest;import com.android.volley.toolbox.StringRequest;import com.android.volley.toolbox.Volley;import org.json.JSONObject;public class VolleyUsing extends AppCompatActivity { Context context; RequestQueue queue; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_volley_using); context=VolleyUsing.this; queue= Volley.newRequestQueue(context);//新建Volley的请求队列 } public void Request(View v){ StringRequest stringRequest=new StringRequest("http://www.news.ynu.edu.cn/", new Response.Listener<String>() { @Override public void onResponse(String response) {//返回成功,打印到控制台 Log.d("TAG", response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.e("TAG", error.getMessage(), error); } }); queue.add(stringRequest); Log.i("数据请求成功","------返回值-----"); } public void Request1(View v){ JsonObjectRequest JoRequest=new JsonObjectRequest("http://api.kanzhihu.com/getposts",null, new Response.Listener<JSONObject>() {//返回成功,打印到控制台 @Override public void onResponse(JSONObject response) { Log.d("------返回成功------", response.toString()); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.e("------返回失败------", error.getMessage(), error); } }); queue.add(JoRequest); } public void Request2(View v){ GsonRequest<Weather> gsonRequest=new GsonRequest<Weather>( "http://www.weather.com.cn/data/sk/101010100.html",Weather.class, new Response.Listener<Weather>() { @Override public void onResponse(Weather weather) {//返回成功,打印到控制台 WeatherInfo weatherInfo = weather.getInfo(); Log.d("=======城市信息输出========", "城市为: " + weatherInfo.getCity()); Log.d("=======Temp输出========", "temp is " + weatherInfo.getTemp()); Log.d("=======时间信息输出========", "time is " + weatherInfo.getTime()); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.e("TAG", error.getMessage(), error); } }); queue.add(gsonRequest); }}
最后,大家别忘记加入volley的jar包和网络许可,不然会报错的。
Demo整理好给大家上传。
1 0
- 初识Volley框架 加载以及查看json数据+html元素
- Volley框架的使用(加载网络图片,加载json数据)
- Android 网络通信框架Volley简介以及获取JSON对象和图片加载例子
- Google官方网络框架-Volley的使用解析Json以及加载网络图片方法
- Google官方网络框架-Volley的使用解析Json以及加载网络图片方法
- 网络框架-Volley的使用解析Json以及加载网络图片方法
- 初识Volley框架
- Volley框架初识01
- 初识Volley框架
- 利用Volley和JSON从服务器里加载数据
- android 网络框架 volley 初识
- HTML笔记(HTML元素--框架以及内联框架)
- Volley请求的简单封装,以及applicaation/json数据请求
- Android实战:使用Volley框架访问服务器Json数据
- volley JSON请求数据
- Volley解析Json数据
- volley JSON请求数据
- volley学习笔记(四):Android利用Volley异步加载数据(JSON和图片)完整示例
- 怎样在JavaWeb项目中自定义一个配置文件 dom4j
- iOS--OC--图片拉伸和尺寸变换,图片简单处理
- CAN与CANOpen(四)
- windows 无法找到unistd.h 的解决方法
- 理解LSTM网络
- 初识Volley框架 加载以及查看json数据+html元素
- Idea的全局搜索快捷键不起作用的解决方法
- 洛谷P1656 炸铁路
- Picasso源码解析
- IOS中使用像素位图(CGImageRef)对图片进行处理
- 在java中使用dom4j解析xml(示例代码)
- failed to push some refs to 'git@github.com:songmengke/NotePadVMware.git'
- spark sortByKey
- 简单 Java 加密算法 1.0