1.引入依赖
2.设置网络权限
3.新建一个工具类
3.1在api.java中添加以下代码
3.2 alt+enter 新增interface接口回调函数
4. 在MainActivity.java里输入代码
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;
public class MainActivity extends AppCompatActivity {
private TextView tvWeather;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// TextView tvWeather = findViewById(R.id.tv_weather);
// 鼠标你点到tvWeather 上按ctrl+alt+f 这个快捷键可以生成全局变量 最后点击一下enter就可以了
tvWeather = findViewById(R.id.tv_weather);
// 在这个地方创建一个方法 然后还是点击alt+enter 会自动提示问你是想要创建一个方法么
// 这个键就相当于一个帮助按钮吧,可以补全代码,方法,或者发生错误的时候给你一些提示信息
init();
}
private void init() {
Api.config().getRequest("http://wthrcdn.etouch.cn/weather_mini?citykey=101210101", new ApiCallback() {
@Override
public void onSuccess(String res) {
Log.e("onSuccess", res);
Gson gson = new Gson();
final Weather weather = gson.fromJson(res, Weather.class);
if (weather.getStatus() == 1000) {
// weather
Looper.prepare();
Toast.makeText(MainActivity.this, "获取信息成功", Toast.LENGTH_SHORT).show();
runOnUiThread(new Runnable() {
@Override
public void run() {
//tvWeather.setText("获取一下城市吧:"+weather.getData().getCity());
tvWeather.setText("获取一下城市吧:"+weather.getData().getForecast().get(4).getType());
}
});
Looper.loop();
} else {
Looper.prepare();
Toast.makeText(MainActivity.this, "获取信息失败", Toast.LENGTH_SHORT).show();
Looper.loop();
}
}
@Override
public void onFailure(Exception e) {
Log.e("onFailure", e.getMessage());
Looper.prepare();
Toast.makeText(MainActivity.this, "连接失败", Toast.LENGTH_SHORT).show();
Looper.loop();
}
});
}
// private void init() {
//// 在这里使用刚才创建的那个工具类里的方法
// Api.config().getRequest("http://wthrcdn.etouch.cn/weather_mini?citykey=101210101", new ApiCallback() {
// @Override
// public void onSuccess(String res) {
//// 成功的时候在这里可以写你想要的操作
//// res 这个就是获取到的result
//// 现在解析数据 用Gson解析 引入一下他的依赖
// Gson gson = new Gson();
// final Weather weather = gson.fromJson(res, Weather.class);
//// 返回的数据中有一个 "status": 1000, 判断数据是否请求成功
// if(weather.getStatus() == 1000){
// Log.i("获取成功了", "onSuccess: "+weather.getData().getCity());
//// 因为okhttp获取数据是异步的所以是在子线程中进行的 但是UI操作只能在主线程中进行所以需要用runOnUiThread开一个线程进入到主线程中显示数据
// Looper.prepare();
// Toast.makeText(MainActivity.this, "获取信息成功", Toast.LENGTH_SHORT).show();
// Looper.loop();
// runOnUiThread(new Runnable() {
// @Override
// public void run() {
// tvWeather.setText("获取个城市吧:"+weather.getData().getCity());
// }
// });
// }else {
//// 这个也toast是要在主线程中
//// 用另一种方法写一下 给他开一个队列
// Looper.prepare();
// Toast.makeText(MainActivity.this, "获取信息错误", Toast.LENGTH_SHORT).show();
// Looper.loop();
// }
// }
//
// @Override
// public void onFailure(Exception e) {
// Looper.prepare();
// Toast.makeText(MainActivity.this, "错误"+e.getMessage(), Toast.LENGTH_SHORT).show();
// Looper.loop();
// }
// });
// }
}
5. URL地址是http开头的所以要求添加一个 禁用掉明文流量请求的检查
6.1 新增依赖
implementation 'com.google.code.gson:gson:2.7'
引入依赖
6.2 Android studio插件下载(GsonFormat)
7. 新建weather类
alt+s ,打开弹框,黏贴入接口返回的数据,format格式化数据,点击ok,自动生成代码