• android开发连接网络


    1、申请云服务器windows版
    2、开通云服务器远程连接
    3、通过远程桌面连接云服务器
    4、在云服务器上安装Tomcat、cpolar内网穿透发布Tomcat服务器内容
    5、android项目build.gradle注入依赖:
    dependencies {

    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.8.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation files('libs\\gson-2.9.1.jar')
    implementation 'com.squareup.okhttp3:okhttp:3.5.0'
    implementation 'com.squareup.okio:okio:1.11.0'
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    // implementation files(‘libs\glide-4.4.0.jar’)
    implementation ‘com.nineoldandroids:library:2.4.0’
    implementation ‘com.github.bumptech.glide:glide:3.7.0’
    testImplementation ‘junit:junit:4.13.2’
    androidTestImplementation ‘androidx.test.ext:junit:1.1.5’
    androidTestImplementation ‘androidx.test.espresso:espresso-core:3.5.1’

    }

    重点是okhttp3:
    implementation ‘com.squareup.okhttp3:okhttp:3.5.0’
    implementation ‘com.squareup.okio:okio:1.11.0’

    6、获取信息:
    private void initData() {
    OkHttpClient okHttpClient = new OkHttpClient();
    Request request = new Request.Builder().url(WEB_SITE +
    REQUEST_GOODS_URL).build();

        Call call = okHttpClient.newCall(request);
    
        Toast.makeText(MainActivity.this, call.request().toString(), Toast.LENGTH_LONG).show();
    
    • 1
    • 2
    • 3

    // 开启异步线程访问网络
    call.enqueue(new Callback() {
    @Override
    public void onResponse(Call call, Response response) throws IOException {
    Log.i(“fff”,“kkkkkk”);
    // Toast.makeText(MainActivity.this, response.toString(), Toast.LENGTH_LONG).show();
    String res = response.body().string(); // 获取商品数据
    Message msg = new Message();
    msg.what = MSG_GOODS_OK;
    msg.obj = res;
    mHandler.sendMessage(msg);
    }
    @Override
    public void onFailure(Call call, IOException e) {
    Log.i(“fff”,“jjjjjjj”);

            }
        });
    }
    
    • 1
    • 2
    • 3
  • 相关阅读:
    143.如何个性化推荐系统设计-3
    SSM花艺商城系统毕业设计-附源码171536
    计算机网络知识点(七)
    vue的孙组件获取祖组件数据的方法(this.$parent)
    [C++]使用纯opencv去部署yolov9的onnx模型
    专业级操作,如何快速批量虚化多个视频的背景边框
    Hive Thrift Server
    Cmake学习
    图解TCP/IP(第五版)&& 计算机网络 -- 学习笔记和读后感(学习补充中~~~)
    百度测开面试题分享
  • 原文地址:https://blog.csdn.net/cuisidong1997/article/details/134490448