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; } }