android okhttp框架get请求

 // 开启线程,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();

    }
发布了82 篇原创文章 · 获赞 2 · 访问量 9583

猜你喜欢

转载自blog.csdn.net/julicliy/article/details/104107955