Java JSON字段没有数据,或无法获取适当的数据
我正在为我的android编程学校项目编写这段代码,我遇到了一个问题,JSON似乎在从API中检索一些数据时出错 这是我正在开发的一个取水应用程序,我正在使用openweathermap.org提供的免费API,同时引用来自的代码。不同之处在于,我没有使用活动和片段,而是将片段和活动结合在一起,并做了一些小的修改,使其在Eclipse上可以运行 下面是我出错的代码:Java JSON字段没有数据,或无法获取适当的数据,java,android,json,eclipse,Java,Android,Json,Eclipse,我正在为我的android编程学校项目编写这段代码,我遇到了一个问题,JSON似乎在从API中检索一些数据时出错 这是我正在开发的一个取水应用程序,我正在使用openweathermap.org提供的免费API,同时引用来自的代码。不同之处在于,我没有使用活动和片段,而是将片段和活动结合在一起,并做了一些小的修改,使其在Eclipse上可以运行 下面是我出错的代码: private void updateWeatherInfo(final String city) { new T
private void updateWeatherInfo(final String city) {
new Thread(){
public void run(){
final JSONObject json = FetchData.getJSON(MainActivity.this, city);
if(json == null) {
handleWeather.post(new Runnable() {
public void run() {
Toast.makeText(MainActivity.this, R.string.place_not_found, Toast.LENGTH_LONG).show();
}
});
}
else {
handleWeather.post(new Runnable() {
public void run() {
renderWeather(json);
}
});
}
}
}.start();
}
private void renderWeather(JSONObject json) {
try {
cityData.setText(json.getString("name").toUpperCase(Locale.US) + ", " + json.getJSONObject("sys").getString("country"));
JSONObject details = json.getJSONArray("weather").getJSONObject(0);
JSONObject main = json.getJSONObject("main");
detailsData.setText(details.getString("description").toUpperCase(Locale.US) + "\n" + "Humidity: " + main.getString("humidity") + "%" + "\n" + "Pressure: " + main.getString("pressure") + " hPa");
currentTemperatureData.setText(String.format("%.2f", main.getDouble("temp"))+ " ℃");
DateFormat df = DateFormat.getDateTimeInstance();
String updatedOn = df.format(new Date(json.getLong("dt")*1000));
updatedData.setText("Last update: " + updatedOn);
getWeatherIcon(details.getInt("id"), json.getJSONObject("sys").getLong("sunrise") * 1000, json.getJSONObject("sys").getLong("sunset") * 1000);
}
catch(Exception e) {
Log.e("SimpleWeather", "One or more fields not found in the JSON data");
}
}
try/catch中的异常会在到达cityData和detailsData后继续发生。我知道这一点,因为当我调试它并到达那里时,它会在几步之后直接进入catch,并且在注释掉cityData之后,下次它再次被路由到catch时是在detailsData
有谁能看出哪里出了问题并帮我指出吗
编辑:这是我使用的API-
EDIT2:这似乎是因为初始化文本视图的onCreateView在UpdateWaterData之后,而我在调用UpdateWaterData之前首先移动了它。然而,现在我意识到我的onCreateView也没有@Override,在加入@Override后,出现了一个错误,它必须实现一个超类型方法
守则如下:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_main, container, false);
cityData = (TextView)rootView.findViewById(R.id.city_field);
updatedData = (TextView)rootView.findViewById(R.id.updated_field);
detailsData = (TextView)rootView.findViewById(R.id.details_field);
currentTemperatureData = (TextView)rootView.findViewById(R.id.current_temperature_field);
weatherIcon = (TextView)rootView.findViewById(R.id.weather_icon);
weatherIcon.setTypeface(weatherIcons);
return rootView;
}
我知道这段代码是为一个片段编写的,但将其实现为一个活动似乎我在这里修改它时错过了一步。我如何从这里开始
EDIT3:我现在已经在onCreate中实现了onCreateView数据,因为onCreateView是onCreate的片段版本。然而,我遇到了另一个超出这个问题范围的问题,因为我的setTypeface返回一个NullPointerException。其代码如下所示:
public class MainActivity extends Activity {
TextView cityData;
TextView updatedData;
TextView detailsData;
TextView currentTemperatureData;
TextView weatherIcon;
Typeface weatherIcons;
Handler handleWeather;
public MainActivity() {
handleWeather = new Handler();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
cityData = (TextView) findViewById(R.id.city_field);
updatedData = (TextView) findViewById(R.id.updated_field);
detailsData = (TextView) findViewById(R.id.details_field);
currentTemperatureData = (TextView) findViewById(R.id.current_temperature_field);
weatherIcon = (TextView) findViewById(R.id.weather_icon);
weatherIcons = Typeface.createFromAsset(getAssets(), "weather.ttf");
weatherIcon.setTypeface(weatherIcons);
因此,我将继续我的搜索这个问题一段时间,如果我找到我的答案,我不会张贴另一个问题。但是,我将按照回答结束这个问题。可能cityData和detailsData为空。请确保在调用updatewatherinfo方法@ρ砦σѕρєK之前初始化这两个变量:我已将初始化向上移动到updatewatherinfo之前,但仍然在相同的点发生异常。