Volley下载数据显示在RecyclerView上 需要注册和添加网络权限

package com.example.administrator.month_exam_20;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.example.administrator.month_exam_20.demo.RecAdapter;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class Main2Activity extends AppCompatActivity {

    private List<Bean> list =new ArrayList<>();
    private RecyclerView rec;
    private RecAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        rec = (RecyclerView) findViewById(R.id.rec);

        LinearLayoutManager manager =new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        adapter = new RecAdapter(list,this);

        rec.setLayoutManager(manager);
        rec.setAdapter(adapter);
        volletGet();


    }

    private void volletGet() {
        String path ="http://apis.juhe.cn/idcard/index?key=bb97bfce9edee938aeac99cb503b76db&cardno=430524199106158690";

        StringRequest request =new StringRequest(Request.Method.GET,path, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                Log.e("MainActivity","==="+s);

                Gson gson =new Gson();

                Bean bean = gson.fromJson(s, Bean.class);
                list.add(bean);
                adapter.notifyDataSetChanged();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        });

MyApp.getHttpQueue().add(request);

    }
}
 
package com.example.administrator.month_exam_20;

import android.app.Application;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;

/**
 * Created by Administrator on 2018/1/18.
 */

public class MyApp extends Application {

    public static RequestQueue queue;

    @Override
    public void onCreate() {
        super.onCreate();
        queue= Volley.newRequestQueue(getApplicationContext());
    }
    public static RequestQueue getHttpQueue(){


        return queue;
    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值