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'
// 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();
// 开启异步线程访问网络
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”);
}
});
}