• 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
  • 相关阅读:
    9月大型语言模型研究论文总结
    Spring Boot 教程:使用 Spring Boot 整合 JdbcTemplate
    Java 压缩PDF文档
    EasyExcel 简单导入/导出 Controller Demo
    工业智能中的组态
    【水滴计划】:合并两个有序数组、寻找两个正序数组的中位数
    A child container failed during start之解决方法
    spring-security源码学习总结
    ROS从入门到精通(八) 常用传感器与消息数据
    使用 EMQX 接入 MQTT-SN 协议设备
  • 原文地址:https://blog.csdn.net/cuisidong1997/article/details/134490448