package com
.example
.tingyu
;
import android
.os
.Bundle
;
import android
.view
.View
;
import android
.widget
.Button
;
import android
.widget
.TextView
;
import android
.widget
.Toast
;
import androidx
.appcompat
.app
.AppCompatActivity
;
import com
.google
.gson
.Gson
;
import com
.google
.gson
.reflect
.TypeToken
;
import com
.zhy
.http
.okhttp
.OkHttpUtils
;
import com
.zhy
.http
.okhttp
.callback
.StringCallback
;
import java
.lang
.reflect
.Type
;
import java
.util
.List
;
import okhttp3
.Call
;
import okhttp3
.OkHttpClient
;
public class tingyu extends AppCompatActivity {
String url
="https://tianqiapi.com/api?version=v6&appid=56794388&appsecret=5cPdrXIj";
private TextView city
;
private TextView wea
;
private TextView win
;
private TextView tem
;
@Override
protected void onCreate(Bundle savedInstanceState
) {
OkHttpClient o
=new OkHttpClient();
super.onCreate(savedInstanceState
);
setContentView(R
.layout
.activity_tingyu
);
city
= findViewById(R
.id
.ctiy
);
wea
= findViewById(R
.id
.wea
);
win
= findViewById(R
.id
.win
);
tem
= findViewById(R
.id
.tem
);
find();
}
private void find() {
OkHttpUtils
.get()
.url(url
)
.build()
.execute(new StringCallback() {
@Override
public void onError(Call call
, Exception e
, int i
) {
}
@Override
public void onResponse(String s
, int i
) {
gson(s
);
}
});
}
public void gson(String a
)
{
String json
=a
;
Gson gson2
=new Gson();
gson gson
= gson2
.fromJson(json
, gson
.class);
city
.setText("城市:"+gson
.getCity());
wea
.setText("天气情况:"+gson
.getWea());
win
.setText("风向:"+gson
.getWin());
tem
.setText("温度:"+gson
.getTem());
}
}
gson业务类:
package com
.example
.tingyu
;
public class gson {
private String city
;
private String win
;
private String tem
;
private String wea
;
public String
getCity() {
return city
;
}
public void setCity(String city
) {
this.city
= city
;
}
public String
getWin() {
return win
;
}
public void setWin(String win
) {
this.win
= win
;
}
public String
getTem() {
return tem
;
}
public void setTem(String tem
) {
this.tem
= tem
;
}
public String
getWea() {
return wea
;
}
public void setWea(String wea
) {
this.wea
= wea
;
}
}
接口在这里申请就可以了https://tianqiapi.com/
转载请注明原文地址:https://ipadbbs.8miu.com/read-36213.html