我正在尝试获取温度和条件等天气详情,我无法显示它们但是我能够在我的日志猫代码中看到它们。
以下是我的代码
cond = (TextView) findViewById(R.id.condDescr);
temp = (TextView) findViewById(R.id.tempText);
temp.setText("HI How are you");
JSONWeatherTask task = new JSONWeatherTask();
task.execute(new String[]{city});
private class JSONWeatherTask extends AsyncTask<String, Void, Weather> {
@Override
protected Weather doInBackground(String... params) {
Weather weather = new Weather();
String data = ( (new WeatherHttpClient()).getWeatherData(params[0]));
try {
weather = JSONWeatherParser.getWeather(data);
// Let's retrieve the icon
//weather.iconData = ( (new WeatherHttpClient()).getImage(weather.currentCondition.getIcon()));
} catch (JSONException e) {
e.printStackTrace();
}
return weather;
}
@Override
protected void onPostExecute(Weather weather) {
super.onPostExecute(weather);
if (weather.iconData != null && weather.iconData.length > 0) {
Bitmap img = BitmapFactory.decodeByteArray(weather.iconData, 0, weather.iconData.length);
//imgView.setImageBitmap(img);
}
String str1 = (weather.currentCondition.getCondition() + "(" + weather.currentCondition.getDescr() + ")");
//temp.setText("" + Math.round((weather.temperature.getTemp() - 273.15)) + "�C");**(I have tried this one too)**
String str2 = ("" + weather.currentCondition.getHumidity() + "%");
Log.w("myApp", str1);
Log.w("mytemp", str2);
cond.setText(str1);
temp.setText(str2);
//temp.setText("" + Math.round((weather.temperature.getTemp() - 273.15)) + "�C");
}
我的XML文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageButton
android:id="@+id/but_wifi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#000000"
android:onClick="onButtonClick"
android:src="@drawable/ic_start_wifi_on" />
<ImageButton
android:id="@+id/but_start_gps"
android:src="@drawable/ic_start_gps_on" />
<View
android:id="@+id/view_mid"
android:layout_width="fill_parent"
android:layout_height="90dp"
android:layout_centerVertical="true" />
<Button
android:id="@+id/but_chooseTrack"
android:textColor="#FFFFFF" />
<Button
android:id="@+id/but_go"
android:layout_width="190dp"
android:text="@string/bt_txt_go"
android:textColor="#FFFFFF" />
<TextView
android:id="@+id/dateText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/but_start_gps"
android:layout_centerHorizontal="true"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/tempText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/dateText"
android:layout_below="@+id/dateText"
android:layout_marginTop="16dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="15dp"
android:layout_toRightOf="@+id/but_wifi"
android:src="@drawable/weather" />
</RelativeLayout>
我也尝试过手动数据集,但它对我不起作用。我不知道我的错误是什么,任何人都可以告诉我如何解决这个问题。
答案 0 :(得分:2)
要调用ASyncTask的方法doInBackground
,您需要添加task.execute(params)
。
params
可以是String
或多个Strings
,由
在您的情况下,您应该传递执行getWeatherData()方法所需的数据。