Android 无法使用改型正确解析Json
我不明白我的json解析为什么不起作用。这是我正在使用的Api。还有完整json输出的链接 我试着从每天中获得最低和最高温度。这是我的密码。引发的异常应为BEGIN_对象,但在第1行第190列为数字。非常感谢您的帮助,谢谢Android 无法使用改型正确解析Json,android,json,retrofit,Android,Json,Retrofit,我不明白我的json解析为什么不起作用。这是我正在使用的Api。还有完整json输出的链接 我试着从每天中获得最低和最高温度。这是我的密码。引发的异常应为BEGIN_对象,但在第1行第190列为数字。非常感谢您的帮助,谢谢 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activit
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView =(ListView) findViewById(R.id.main_activity_list);
arrayAdapter = new ArrayAdapter<>(
this,
android.R.layout.simple_list_item_1,
android.R.id.text1,
new ArrayList<List1>());
listView.setAdapter(arrayAdapter);
}
@Override
protected void onResume() {
super.onResume();
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://api.openweathermap.org")
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build();
WeatherAPI weatherAPI = retrofit.create(WeatherAPI.class);
Call<Weather> call = weatherAPI.loadWeather("85008", "json", "metric", "7", "3c6fee6e3e8b5764212701d9535a36d5");
call.enqueue(this);
}
@Override
public void onResponse(Call<Weather> call, Response<Weather> response) {
arrayAdapter.clear();
arrayAdapter.addAll(response.body().list);
}
@Override
public void onFailure(Call<Weather> call, Throwable t) {
Log.v(MainActivity.class.getSimpleName(), t.getLocalizedMessage());
}
public interface WeatherAPI {
@GET("/data/2.5/forecast/daily")
Call<Weather> loadWeather(
@Query("zip")String zip,
@Query("amode")String amode,
@Query("units")String units,
@Query("cnt")String cnt,
@Query("APPID")String APIKey);
}
public class Weather{
public List<List1> list;
}
public class List1{
double dt;
public HashMap<String, Temps> temp;
@Override
public String toString() {
String output = "Min and High ";
for(Map.Entry<String,Temps> temps:temp.entrySet()){
output += temps.getKey() + " = " + temps.getValue().min;
}
return output;
}
}
public class Temps{
double min;
double max;
}
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView ListView=(ListView)findViewById(R.id.main\u activity\u list);
arrayAdapter=新的arrayAdapter(
这
android.R.layout.simple\u list\u item\u 1,
android.R.id.text1,
新的ArrayList());
setAdapter(arrayAdapter);
}
@凌驾
受保护的void onResume(){
super.onResume();
HttpLoggingInterceptor拦截器=新的HttpLoggingInterceptor();
拦截器.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient客户端=新建OkHttpClient.Builder().addInterceptor(拦截器).build();
改装改装=新改装.Builder()
.baseUrl(“http://api.openweathermap.org")
.客户(客户)
.addConverterFactory(GsonConverterFactory.create())
.build();
WeatherAPI WeatherAPI=改装.create(WeatherAPI.class);
Call Call=weatherAPI.loadWeather(“85008”、“json”、“公制”、“7”、“3c6fee6e3e8b5764212701d9535a36d5”);
call.enqueue(这个);
}
@凌驾
公共void onResponse(调用、响应){
arrayAdapter.clear();
arrayAdapter.addAll(response.body().list);
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Log.v(MainActivity.class.getSimpleName(),t.getLocalizedMessage());
}
公共接口WeatherAPI{
@获取(“/data/2.5/forecast/daily”)
呼叫loadWeather(
@查询(“zip”)字符串zip,
@查询(“amode”)字符串amode,
@查询(“单位”)字符串单位,
@查询(“cnt”)字符串cnt,
@查询(“APPID”)字符串(密钥);
}
公共天气{
公开名单;
}
公共类列表1{
双dt;
公共HashMap temp;
@凌驾
公共字符串toString(){
字符串输出=“最小和高”;
对于(Map.Entry temps:temp.entrySet()){
输出+=temps.getKey()+“=”+temps.getValue().min;
}
返回输出;
}
}
公共课临时工{
双分钟;
双峰;
}
多亏了亚赞,答案是
public class Weather{
public List<List1> list;
}
public class List1{
double dt;
public Temps temp;
@Override
public String toString() {
return "List1{" +
"temp min = " + temp.min + " temp max " + temp.max + " dt = "+ dt+
'}';
}
}
public class Temps{
double min;
double max;
}
公共类天气{
公开名单;
}
公共类列表1{
双dt;
公共临时工;
@凌驾
公共字符串toString(){
返回“List1{”+
“temp min=“+temp.min+”temp max“+temp.max+”dt=“+dt+
'}';
}
}
公共课临时工{
双分钟;
双峰;
}
}
其中temp不是由hashmap显示的,而是由单个对象显示的。我希望这对我认识的人有帮助,这对我有帮助 temp
不是数组,而是它的类对象
有一点您应该知道,改型将不支持直接哈希映射检索(Pojo方法)
公共HashMap temp代码>--这种方法是错误的
public temp代码>--这是正确的
如果您想在HashMap
中存储您的响应,还有一些其他的解决方法,您应该看看。您可以分享WeatherAPI吗?我认为在list1类中将Temp表示为哈希是不对的,它应该是一个对象Temps Temp代码>因为在json中,每个列表项有1个temp
,就是这样!谢谢亚赞
public class Weather{
public List<List1> list;
}
public class List1{
double dt;
public Temps temp;
@Override
public String toString() {
return "List1{" +
"temp min = " + temp.min + " temp max " + temp.max + " dt = "+ dt+
'}';
}
}
public class Temps{
double min;
double max;
}