вылетает приложенния (Retrofit)
Не понимаю что делаю не так
Вот ошибки
2021-01-25 19:39:27.366 28218-28218/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.weather, PID: 28218
java.lang.NullPointerException: Attempt to invoke virtual method 'com.example.weather.POJO.Main com.example.weather.POJO.Example.getMain()' on a null object reference
at com.example.weather.MainActivity$1.onResponse(MainActivity.java:109)
at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1.lambda$onResponse$0$DefaultCallAdapterFactory$ExecutorCallbackCall$1(DefaultCallAdapterFactory.java:89)
at retrofit2.-$$Lambda$DefaultCallAdapterFactory$ExecutorCallbackCall$1$hVGjmafRi6VitDIrPNdoFizVAdk.run(Unknown Source:6)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6671)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:495)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:860)
2021-01-25 19:39:27.384 28218-28218/? I/Process: Sending signal. PID: 28218 SIG: 9
MainActivity
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.TextView;
import com.example.weather.POJO.Example;
import com.example.weather.POJO.Main;
import org.json.JSONException;
import org.json.JSONObject;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class MainActivity extends AppCompatActivity {
LocationManager locationManager;
private static final int REQUEST_CODE = 1;
static double latitude;
private double longtitude;
TextView textView;
@SuppressLint("MissingPermission")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
if(!checkPermission()){
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE);
} else {
getLocation();
}
}
private boolean checkPermission(){
int FineLocation = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
int CoarseLocation = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
if(FineLocation != PackageManager.PERMISSION_GRANTED && CoarseLocation != PackageManager.PERMISSION_GRANTED){
return false;
} else{
return true;
}
} else{
return true;
}
}
private void getWeather(){
final String API = "0fcdeed0b44f8572a682c8837f51a541";
final String CELSIUS = "metric";
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://api.openweathermap.org/data/2.5/")
.addConverterFactory(GsonConverterFactory.create())
.build();
CurrentWeather currentWeather = retrofit.create(CurrentWeather.class);
Call<Example> exampleCall = currentWeather.weather(latitude, longtitude, API);
exampleCall.enqueue(new Callback<Example>() {
@Override
public void onResponse(Call<Example> call, Response<Example> response) {
Example myData = response.body();
Main main = myData.getMain();
double temp = main.getTemp();
textView.setText(String.valueOf(temp));
}
@Override
public void onFailure(Call<Example> call, Throwable t) {
}
});
}
@SuppressLint("MissingPermission")
private void getLocation(){
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
Location location = locationManager.getLastKnownLocation(locationManager.NETWORK_PROVIDER);
locationListener.onLocationChanged(location);
getWeather();
}
private LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(@NonNull Location location) {
if(location != null) {
latitude = location.getLatitude();
longtitude = location.getLongitude();
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(@NonNull String provider) {
}
@Override
public void onProviderDisabled(@NonNull String provider) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("GPS disable!");
builder.setMessage("Do you want enable GPS?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//start settings window when user can enable GPS
startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//close AlertDialog
dialog.cancel();
}
});
AlertDialog enableGps = builder.create();
enableGps.show();
}
};
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if(grantResults.length > 0 && grantResults[0] != PackageManager.PERMISSION_GRANTED){
finish();
} else {
}
}
}
CurrentWeather
package com.example.weather;
import com.example.weather.POJO.Example;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;
public interface CurrentWeather {
@GET("wearther?")
Call<Example> weather(@Query("lat=") double latitude,
@Query("&lon=") double longtitude,
@Query("&appid=") String api);
}
Example
package com.example.weather.POJO;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.util.List;
public class Example {
@SerializedName("weather")
@Expose
private List<Weather> weather = null;
@SerializedName("main")
@Expose
public Main main;
@SerializedName("name")
@Expose
public String name;
public List<Weather> getWeather() {
return weather;
}
public void setWeather(List<Weather> weather) {
this.weather = weather;
}
public Main getMain() {
return main;
}
public void setMain(Main main) {
this.main = main;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Main
package com.example.weather.POJO;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Main {
@SerializedName("temp")
@Expose
public double temp;
@SerializedName("feels_like")
@Expose
private double feelsLike;
@SerializedName("temp_min")
@Expose
private double tempMin;
@SerializedName("temp_max")
@Expose
private double tempMax;
@SerializedName("pressure")
@Expose
private int pressure;
@SerializedName("humidity")
@Expose
private int humidity;
@SerializedName("sea_level")
@Expose
private int seaLevel;
@SerializedName("grnd_level")
@Expose
private int grndLevel;
public double getTemp() {
return temp;
}
public void setTemp(double temp) {
this.temp = temp;
}
public double getFeelsLike() {
return feelsLike;
}
public void setFeelsLike(double feelsLike) {
this.feelsLike = feelsLike;
}
public double getTempMin() {
return tempMin;
}
public void setTempMin(double tempMin) {
this.tempMin = tempMin;
}
public double getTempMax() {
return tempMax;
}
public void setTempMax(double tempMax) {
this.tempMax = tempMax;
}
public int getPressure() {
return pressure;
}
public void setPressure(int pressure) {
this.pressure = pressure;
}
public int getHumidity() {
return humidity;
}
public void setHumidity(int humidity) {
this.humidity = humidity;
}
public int getSeaLevel() {
return seaLevel;
}
public void setSeaLevel(int seaLevel) {
this.seaLevel = seaLevel;
}
public int getGrndLevel() {
return grndLevel;
}
public void setGrndLevel(int grndLevel) {
this.grndLevel = grndLevel;
}
}
Weather
package com.example.weather.POJO;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Weather {
@SerializedName("id")
@Expose
private int id;
@SerializedName("main")
@Expose
private String main;
@SerializedName("description")
@Expose
private String description;
@SerializedName("icon")
@Expose
private String icon;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getMain() {
return main;
}
public void setMain(String main) {
this.main = main;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
}
Ответы (1 шт):
Автор решения: KingKoval
→ Ссылка
@Query использовал лишние символы уже от безысходности. Проблема была в @GET вместо @GET("weather") я написал @GET("wearther")