初识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