Я пытаюсь сделать метод GET, который получает JSON с модернизацией, но он дает мне ошибку.Я не знаю, является ли это потому, что я неправильно передаю параметр или класс Object не определен правильно.
public class MainActivity extends AppCompatActivity {
Retrofit retrofit;
WeatherAPI weatherAPI;
String locationCurrent = "Barcelona";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
retrofit = new Retrofit.Builder()
.baseUrl("http://api.apixu.com/v1/")
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
weatherAPI =retrofit.create(WeatherAPI.class);
weatherGET(locationCurrent);
}
void weatherGET(final String location){
Call<Object> call = weatherAPI.getWeather(location);
call.enqueue(new Callback<Object>() {
@Override
public void onResponse(Call <Object> call, Response<Object> response) {
Log.d("%%%%%","Entrada");
Object dataGETWeather;
dataGETWeather = response.body(); Log.d("Temp",String.valueOf(dataGETWeather.CurrentObject.getTemp_c()));
}
@Override
public void onFailure(Call<Object> call, Throwable t) {
Log.d("%%%%%","Salida");
}
});
}
}
Определение службы:
public interface WeatherAPI {
@GET("current.json?key=9aa4f8a0b09c4034b44183812191306&q={location}")
@Headers("Accept:application/json")
Call<Object> getWeather(@Query("location") String location);
}
Получаемый объект:
// The father of the object is this, but it has more subdivisions
public class Object {
Location LocationObject;
Current CurrentObject;
// Getter Methods
public Location getLocation() {
return LocationObject;
}
public Current getCurrent() {
return CurrentObject;
}
// Setter Methods
public void setLocation(Location locationObject) {
this.LocationObject = locationObject;
}
public void setCurrent(Current currentObject) {
this.CurrentObject = currentObject;
}
}
Журнал исключения:
019-06-14 13:36:36.236 14987-14987/com.example.weatherapiE/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.weatherapi, PID: 14987
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.weatherapi/com.example.weatherapi.MainActivity}: java.lang.IllegalArgumentException: URL query string "key=9aa4f8a0b09c4034b44183812191306&q={location}" must not have replace block. For dynamic query parameters use @Query.
for method WeatherAPI.getWeather
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)