您的位置:首页 > 文旅 > 美景 > 互联网推广招聘_优化方案化学_最新域名解析_收录批量查询工具

互联网推广招聘_优化方案化学_最新域名解析_收录批量查询工具

2025/4/2 17:35:36 来源:https://blog.csdn.net/m0_74225871/article/details/139903318  浏览:    关键词:互联网推广招聘_优化方案化学_最新域名解析_收录批量查询工具
互联网推广招聘_优化方案化学_最新域名解析_收录批量查询工具

一、网络访问

<?xml version="1.0" encoding="utf-8"?>
<network-security-config><domain-config cleartextTrafficPermitted="true"><domain includeSubdomains="true">t.weather.itboy.net</domain></domain-config>
</network-security-config>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET"/><applicationandroid:networkSecurityConfig="@xml/network"android:allowBackup="true"android:dataExtractionRules="@xml/data_extraction_rules"android:fullBackupContent="@xml/backup_rules"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/Theme.MyApplication"tools:targetApi="31"><activityandroid:name=".MainActivity"android:exported="true"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>

重点:

network:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config><domain-config cleartextTrafficPermitted="true"><domain includeSubdomains="true">t.weather.itboy.net</domain></domain-config>
</network-security-config>
<uses-permission android:name="android.permission.INTERNET"/>
android:networkSecurityConfig="@xml/network"

二、代码

public class MainActivity extends AppCompatActivity {TextView textView,textView2;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);EdgeToEdge.enable(this);setContentView(R.layout.activity_main);Button button = findViewById(R.id.button);textView = findViewById(R.id.textView);textView2 = findViewById(R.id.textView2);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {ReClient();}});}private void ReClient(){String url = "http://t.weather.itboy.net/api/weather/city/101260101";OkHttpClient client = new OkHttpClient();Request request = new Request.Builder().url(url).build();client.newCall(request).enqueue(new Callback() {@Overridepublic void onFailure(@NonNull Call call, @NonNull IOException e) {textView.setText(e.toString());}@Overridepublic void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {ReSon(response);}});}private void ReSon(Response response){try (ResponseBody responseBody = response.body()){if (!response.isSuccessful()||responseBody==null){Log.e("22","错误"+response);return;}final String json = responseBody.string();runOnUiThread(new Runnable() {@Overridepublic void run() {//textView.setText(json);GsonJson(json);}});}catch (IOException e){Log.e("22","错误",e);}}private void GsonJson(String json){Gson gson = new Gson();try {MyJson myJson = gson.fromJson(json, MyJson.class);textView.setText(myJson.getDate());textView2.setText(myJson.cityInfo.getCity());}catch (Exception e){Log.e("JsonParsingError", "解析JSON数据时发生错误: " + e.getMessage());}}public class MyJson{private String date;private CityInfo cityInfo;private String getDate() {return date;}private CityInfo getCityInfo() {return cityInfo;}private class CityInfo{private String city;private String getCity() {return city;}}}
}
public class MainActivity extends AppCompatActivity {// 声明TextView控件,用于显示数据TextView textView, textView2;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// 调用自定义方法EdgeToEdge.enable来使界面铺满屏幕边缘到边缘(假设此方法存在并已实现)EdgeToEdge.enable(this);// 设置当前Activity的布局文件setContentView(R.layout.activity_main);// 通过findViewById查找XML布局文件中定义的ButtonButton button = findViewById(R.id.button);// 初始化TextView控件textView = findViewById(R.id.textView);textView2 = findViewById(R.id.textView2);// 为Button设置点击监听器,点击时触发网络请求button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {// 调用发送网络请求的方法ReClient();}});}// 发起网络请求的方法private void ReClient() {// 定义请求的URL,此处为贵阳市的天气APIString url = "http://t.weather.itboy.net/api/weather/city/101260101";// 创建OkHttpClient实例,用于发送网络请求OkHttpClient client = new OkHttpClient();// 构建一个请求对象,设置URLRequest request = new Request.Builder().url(url).build();// 异步发送请求,当请求完成时,会在Callback中处理响应或失败情况client.newCall(request).enqueue(new Callback() {@Overridepublic void onFailure(@NonNull Call call, @NonNull IOException e) {// 当请求失败时,更新textView显示错误信息runOnUiThread(new Runnable() {@Overridepublic void run() {textView.setText(e.toString());}});}@Overridepublic void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {// 请求成功时,调用ReSon方法处理响应数据ReSon(response);}});}// 处理网络响应的方法private void ReSon(Response response) {try (ResponseBody responseBody = response.body()) {// 检查响应是否成功且响应体不为空if (!response.isSuccessful() || responseBody == null) {// 记录错误日志Log.e("22", "错误" + response);return;}// 读取响应体中的字符串数据final String json = responseBody.string();// 在主线程中更新UI,避免UI操作在子线程进行runOnUiThread(new Runnable() {@Overridepublic void run() {// 使用Gson解析JSON数据并更新UIGsonJson(json);}});} catch (IOException e) {// 记录读取响应体时发生的错误Log.e("22", "错误", e);}}// 使用Gson解析JSON数据的方法private void GsonJson(String json) {// 创建Gson实例Gson gson = new Gson();try {// 将JSON字符串转换为MyJson对象MyJson myJson = gson.fromJson(json, MyJson.class);// 从MyJson对象中获取并设置日期到textViewtextView.setText(myJson.getDate());// 获取并设置城市信息到textView2textView2.setText(myJson.getCityInfo().getCity());} catch (Exception e) {// 记录解析JSON数据时发生的错误Log.e("JsonParsingError", "解析JSON数据时发生错误: " + e.getMessage());}}// 自定义数据类,用于映射JSON数据public class MyJson {private String date; // 日期字段private CityInfo cityInfo; // 城市信息对象// Getter方法,获取日期private String getDate() {return date;}// Getter方法,获取城市信息private CityInfo getCityInfo() {return cityInfo;}// 嵌套类,代表城市信息private class CityInfo {private String city; // 城市名称字段// Getter方法,获取城市名称private String getCity() {return city;}}}
}

 

 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com