android okhttp框架get请求

﹥>﹥吖頭↗ 提交于 2020-01-30 03:04:51
 // 开启线程,android 不允许在主线程执行网络任务,所以要开启其他线程,来执行耗时操作,同时利用handler更新UI
    private void getDataFromGet(){
        new Thread(){
            @Override
            public void run() {
                super.run();
                try{
                    String result = get("http://api.m.mtime.cn/PageSubArea/TrailerList.api");
                    Log.d("firstActivity", result);
                    Message msg = Message.obtain();
                    msg.what = GET;
                    msg.obj = result;
                    handler.sendMessage(msg);
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }.start();

    }

    protected String get(String url) throws IOException{

        Request request = new Request.Builder()
                .url(url)
                .build();
        Response response = client.newCall(request).execute();
        return response.body().string();

    }

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!