android - 天气应用程序未显示来自 openweather api 的数据
问题描述
我遇到了这个应用程序的问题。我尝试了一切以使其正常工作,但似乎仍然没有收到任何数据,当我在手机上运行应用程序时,布局会显示,但 textviews 仍然没有收到任何数据,我不知道通过 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);
}
}
}
解决方案
推荐阅读
- javascript - React konva 在渲染数组时仅对 1 个矩形应用 x 变化
- netsuite - 从 netsuite 为客户获取子列表
- c# - 即使我强制 .include 延迟加载也无法正常工作
- javascript - SVG 或 Canvas,如何按坐标和 div 大小切割正方形
- c++ - 有效地查找 Boost BGL 图中的所有可到达顶点
- google-chrome - WebGl 无法在“WebGLRenderingContext”上执行“texImage2D”
- python - 从熊猫数据框中选择所有非空行
- amazon-web-services - 具有静态 IP 的 Amazon EKS 中的 ingress-nginx
- laravel - 如何禁用 Homestead 中的串行日志记录?
- python - 如何从 Python 中的不同文件更改函数中变量的值?