Погода приложение не показывает данные из Openweather API - PullRequest
0 голосов
/ 03 декабря 2018

Я столкнулся с проблемой с этим приложением.Я перепробовал все, чтобы это работало, но кажется, что я не получаю никаких данных, когда я запускаю приложение на телефоне, отображается макет, но текстовые представления остаются такими же, не получая никаких данных, я не знаю, проблема с кодом, хотя GPSактивирован, я попробовал коды API, и они работают, я попробовал API на почтальоне, и он работает, поэтому я не знаю, где может быть проблема, спасибо.

public class MainActivity extends AppCompatActivity implements LocationListener {
    TextView textcity, textlastupdated, textdesciption, texthumidity, textmintemp, textmaxtemp,
            texttime, textcelsius, textpressure;
    ImageView imageView;
    String provider;
    Openweather openweather = new Openweather();
    LocationManager locationManager;
    static double lat;
    static double lon;
    int mypermission = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textcelsius = findViewById(R.id.celius);
        textcity = findViewById(R.id.city);
        textlastupdated = findViewById(R.id.lastupdated);
        textdesciption = findViewById(R.id.description);
        texthumidity = findViewById(R.id.humidity);
        textmaxtemp = findViewById(R.id.maxtemo);
        textmintemp = findViewById(R.id.mintemp);
        textpressure = findViewById(R.id.pressure);
        imageView = findViewById(R.id.imageview);
        texttime = findViewById(R.id.time);
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        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) {
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{
                    Manifest.permission.INTERNET,
                    Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.ACCESS_NETWORK_STATE,
                    Manifest.permission.SYSTEM_ALERT_WINDOW,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE
            }, mypermission);

        }
        Location location = locationManager.getLastKnownLocation(provider);
        if (location == null) {
            Log.e("TAG", "NO LOCATION...");
        }
    }

    @Override
    public void onLocationChanged(Location location) {
          lat = location.getAltitude();
          lon = location.getLongitude();
           new GetWeather().execute(Common.api_request(String.valueOf(lat),String.valueOf(lon)));

    }
    @Override
    protected void onPause() {
        super.onPause();
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{
                    Manifest.permission.INTERNET,
                    Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.ACCESS_NETWORK_STATE,
                    Manifest.permission.SYSTEM_ALERT_WINDOW,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE


            },mypermission);
        }
        locationManager.removeUpdates(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(MainActivity.this,new String[]{
                    Manifest.permission.INTERNET,
                    Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.ACCESS_NETWORK_STATE,
                    Manifest.permission.SYSTEM_ALERT_WINDOW,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE
            },mypermission);

        }
        locationManager.requestLocationUpdates(provider, 400, 1, this);
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }
    public class GetWeather extends AsyncTask<String,Void,String>{
        ProgressDialog pddd = new ProgressDialog(MainActivity.this);

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pddd.setTitle("Please wait...");
            pddd.show();
        }

        @Override
        protected String doInBackground(String... params) {
              String stream = null;
              String uristring = params[0];
              Helper htp = new Helper();
              stream = htp.gethttpupdate(uristring);
            return stream;
        }

        @RequiresApi(api = Build.VERSION_CODES.N)
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            if(s.contains("Error..city not found")){
                pddd.dismiss();
                return;
            }
            Gson gson = new Gson();
            Type type = new TypeToken<Openweather>(){}.getType();
            openweather = gson.fromJson(s,type);
            pddd.dismiss();
            textcity.setText(String.format("%s,%s",openweather.getName(),openweather.getSys().getCountry()));
            textlastupdated.setText(String.format("Last Updated: %s", Common.getdatenow()));
            textdesciption.setText(String.format("%s",openweather.getWeather().get(0).getDescription()));
            texthumidity.setText(String.format("%d%%",openweather.getMain().getHumidity()));
            texttime.setText(String.format("%s/%s",Common.unixtime(openweather.getSys().getSunrise()),Common.unixtime(
                    openweather.getSys().getSunset())));
            textcelsius.setText(String.format("%.2f °C",openweather.getMain().getTemp()));
            Picasso.with(MainActivity.this)
                    .load(Common.getimage(openweather.getWeather().get(0).getIcon()))
                    .into(imageView);

        }
    }
}
...