• OkHttp解析


    public class MainActivity extends AppCompatActivity {
    
        private ListView lv;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            lv = (ListView) findViewById(R.id.lv);
    //        new Thread(
    //                new Runnable() {
    //                    @Override
    //                    public void run() {
    //                        myHttp();
    //                    }
    //                }
    //
    //        ).start();
            asynchronousGet();
        }
        //同步加载
    //    public void myHttp() {
    //        try {
    //            OkHttpClient client=new OkHttpClient();
    //            Request request=new Request.Builder()
    //                    .url("http://apis.juhe.cn/cook/query?key=c4819a55b9837c8159a054e4cd9b76ec&menu=%E8%A5%BF%E7%BA%A2%E6%9F%BF")
    //                    .build();
    //            Call call = client.newCall(request);
    //            Response response = call.execute();
    //            if(response.isSuccessful()){
    //                String string = response.body().string();
    //
    //                Gson gson=new Gson();
    //                Bean bean = gson.fromJson(string, Bean.class);
    //                List<Bean.ResultBean.DataBean> list = bean.getResult().getData();
    //                System.out.println("解析得数据"+list);
    //                lv.setAdapter(new MyAdapter(MainActivity.this,list));
    //            }
    //        } catch (Exception e) {
    //            e.printStackTrace();
    //        }
    //    }
        //异步加载
        public void asynchronousGet(){
    
            OkHttpClient client=new OkHttpClient();
            Request request=new Request.Builder()
                    .url("http://apis.juhe.cn/cook/query?key=c4819a55b9837c8159a054e4cd9b76ec&menu=%E8%A5%BF%E7%BA%A2%E6%9F%BF")
                    .build();
            Call call = client.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
    
                }
    
                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    String string = response.body().string();
                    Gson gson=new Gson();
                    Bean bean = gson.fromJson(string, Bean.class);
                    final List<Bean.ResultBean.DataBean> list = bean.getResult().getData();
                    System.out.println("解析得数据"+list);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            MyAdapter adapter=new MyAdapter(MainActivity.this,list);
                            lv.setAdapter(adapter);
                            //adapter.notifyDataSetChanged();
                        }
                    });
    
                }
            });
    
        }
        }
  • 相关阅读:
    JQuery中的bind()和unbind()的理解
    一行代码实现数组去重(ES6)
    vue.js学习笔记(Vuejs——组件——props数据传递)
    判断一个字符串书否存在某个字符,取出一段字符串括号中的内容
    在vue中使用高德地图vue-amap
    在egg.js中使用mongodb
    使用Element的upload上传组件,不使用action属性上传
    下载和安装mongodb4.2.0+robmongo可视化工具
    数组对象根据某个属性取出重复的个数
    钉钉小程序----使用阿里的F2图表
  • 原文地址:https://www.cnblogs.com/yu12/p/7606306.html
Copyright © 2020-2023  润新知