我遇到了与此应用程序有关的问题。我尽了一切努力使其工作,但似乎仍然没有收到任何数据,当我在手机上运行该应用程序时,布局显示了,但是textview仍然没有收到任何数据,我不知道尽管通过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);
}
}
}