簡體   English   中英

天氣應用(JSON)數據未顯示在textview中

[英]Weather app (JSON) data is not showing in the textview

我在Android Studio上用Java和XML完成了我的weather app,但是當我運行該應用程序時,數據沒有顯示。 我將開放天氣用於JSON數據,並且已經在XML清單中授予訪問位置和Internet的權限。 我只用3個變量測試代碼

Weatherapp.ctemp.setText(String.valueOf(ctempreture));
Weatherapp.ftemp.setText(String.valueOf(ftempreture));
Weatherapp.ktemp.setText(String.valueOf(ktempreture));

我需要使JSON的結果以某種方式出現在我的應用程序中。

以下是主要活動(weatherapp)。

public class Weatherapp extends AppCompatActivity {
static TextView ctemp;
static TextView ftemp;
static TextView ktemp;
static TextView location;
static TextView pressure;
static TextView humidity;
static TextView mintempc;
static TextView mintempf;
static TextView mintempk;
static TextView maxtempc;
static TextView maxtempf;
static TextView maxtempk;
static TextView sealevel;
static TextView groundlevel;
static TextView windspeed;
static Button refresh;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_weatherapp);

    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Dowlode task = new Dowlode();
    String provider = locationManager.getBestProvider(new Criteria(), false);
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    Location locationn = locationManager.getLastKnownLocation(provider);
    Double lat = locationn.getLatitude();
    Double lng = locationn.getLongitude();
    task.execute("https://samples.openweathermap.org/data/2.5/weather?lat=35&lon=139&appid=4ba583d5302c8723764443f9d6f08116");
    ctemp = findViewById(R.id.ctemp);
    ftemp = findViewById(R.id.ftemp);
    ktemp = findViewById(R.id.ktemp);
    location = findViewById(R.id.location);
    pressure = findViewById(R.id.pressure);
    humidity = findViewById(R.id.humidity);
    mintempc = findViewById(R.id.mintempc);
    mintempf = findViewById(R.id.mintempf);
    mintempk = findViewById(R.id.mintempk);
    maxtempc = findViewById(R.id.maxtempc);
    maxtempf = findViewById(R.id.maxtempf);
    maxtempk = findViewById(R.id.maxtempk);
    sealevel = findViewById(R.id.sealevel);
    groundlevel = findViewById(R.id.groundlevel);
    windspeed = findViewById(R.id.windspeed);
    refresh = findViewById(R.id.refresh);
}

以下是我的下載活動代碼

public class Dowlode extends AsyncTask<String,Void,String > {

@Override
protected String doInBackground(String... urls) {
    String result="";
    URL url;
    HttpURLConnection urlConnection =null;

    try {
        url=new URL(urls[0]);
        urlConnection=(HttpURLConnection) url.openConnection();
        InputStream in =urlConnection.getInputStream() ;
        InputStreamReader reader=new InputStreamReader(in);
        int data = reader.read();
        while (data!=-1){
            char current = (char) data;
            result += current;
            data = reader.read();

        }
        return result;
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}

@Override
protected void onPostExecute(String s) {
    super.onPostExecute(s);

    try {
    JSONObject jsonObject = new JSONObject();

    JSONObject weather = new JSONObject(jsonObject.getString("main"));

    double tempreture = Double.parseDouble(weather.getString("temp"));
    int ctempreture = (int) (tempreture- 273.15 );
    int ftempreture = (int) (tempreture * 1.8-459.67);
    int ktempreture = (int) (tempreture);

    Weatherapp.ctemp.setText(String.valueOf(ctempreture));
    Weatherapp.ftemp.setText(String.valueOf(ftempreture));
    Weatherapp.ktemp.setText(String.valueOf(ktempreture));

    String location = jsonObject.getString("name");

    double pressure = Double.parseDouble(weather.getString("pressure"));
    String pressuree = pressure+ "pascal";

    double humidity = Double.parseDouble(weather.getString("humidity"));
    String humidityy= humidity+"";

    double temp_min = Double.parseDouble(weather.getString("temp_min"));
        int cmintemp = (int) (temp_min- 273.15 );
        int fmintemp = (int) (temp_min * 1.8-459.67);
        int kmintemp = (int) (temp_min);

    double temp_max = Double.parseDouble((weather.getString("temp_max")));
        int cmaxtemp = (int) (temp_max- 273.15 );
        int fmaxtemp = (int) (temp_max * 1.8-459.67);
        int kmaxtemp = (int) (temp_max);

    double sea_level = Double.parseDouble(weather.getString("sea_level"));
        int sealevel  = (int) (sea_level);

   double grnd_level = Double.parseDouble((weather.getString("grnd_level")));
        int groundlevel= (int) (grnd_level);

     JSONObject wind = new JSONObject(jsonObject.getString("wind"));

     double speed = Double.parseDouble((wind.getString("speed")));
     int speedd   = (int) (speed);

    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

在執行findViewById之前,您正在執行異步任務,因此,當您嘗試在onPostExecute()方法中設置文本時,可能尚未設置textview的變量。

另外,最好不要在活動中使用靜態變量。 為避免這種情況,您可以將Dowlode類放入活動類中,也可以通過構造函數將活動(或相關視圖)傳遞給Dowlode類。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM