zoukankan      html  css  js  c++  java
  • OkHttpClient

    一.OkHttpClient
    详细简洁:https://github.com/hongyangAndroid/okhttputils

    二.基本示例(Get请求)

    public class MainActivity extends Activity {
    
        TextView tv;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            tv = (TextView)findViewById(R.id.content);
            Log.e("Thread", Thread.currentThread().getId() + "");
            getContent();
        }
    
        private void getContent(){
            OkHttpClient okHttpClient = new OkHttpClient();
            final Request request = new Request.Builder()
                    .url("https://www.baidu.com")
                    .build();
    
            Call call = okHttpClient.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
    
                }
    
                // 异步获取
                @Override
                public void onResponse(Call call, final Response response) throws IOException {
                    // 子线程中
                    if( Thread.currentThread().getId() ==
                            Looper.getMainLooper().getThread().getId() ){
                        Log.d("线程", "Main Thread");
                    }else{
                        Log.d("线程", "Not Main Thread");
                    }
    
                    final String res = response.body().string();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            // 主线程中
                            if( Thread.currentThread().getId() ==
                                    Looper.getMainLooper().getThread().getId() ){
                                Log.d("线程", "Main Thread");
                            }
                            tv.setText(res);
                        }
                    });
                }
            });
    
        }
    }
  • 相关阅读:
    ESP32学习目录
    python中mysql管理模块mysql-connector使用
    MYSQL基础知识和操作
    urlib补充
    Python3中Urllib库是什么?urllib模块基本使用
    递归:斐波契那数列
    python正则模块一
    模块&包
    XML模块示例代码
    使用python操作XML增删改查
  • 原文地址:https://www.cnblogs.com/itfenqing/p/6757148.html
Copyright © 2011-2022 走看看