I am working with retrofit to get the Json data from Url like this http://api.openweathermap.org/data/2.5/weather?id=1277333&appid=00f05ed7a5400d4d7765e69330e28ab4
My interface class is
public interface RetrofitObjectAPI {
String API_KEY="00f05ed7a5400d4d7765e69330e28ab4";
@GET("data/2.5/weather?q=Bangalore,india&appid="+API_KEY)
Call<Weather> getWeatherDetails();
}
My Weather.java
public class Weather {
String name;
String temp;
String pressure;
String humidity;
String wind;
String coord;
public String getName() {
return name;
}
public String getTemp() {
return temp;
}
public String getPressure() {
return pressure;
}
public String getHumidity() {
return humidity;
}
public String getWind() {
return wind;
}
public String getCoord() {
return coord;
}
}
My MainActivity.java
public class MainActivity extends AppCompatActivity {
String url = "http://api.openweathermap.org/";
TextView cityname,temperature,pressure,humidity,wind,coord;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cityname=(TextView)findViewById(R.id.cityText);
temperature=(TextView)findViewById(R.id.temp);
pressure=(TextView)findViewById(R.id.press);
humidity=(TextView)findViewById(R.id.hum);
wind=(TextView)findViewById(R.id.windSpeed);
coord=(TextView)findViewById(R.id.coord);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.build();
RetrofitObjectAPI service = retrofit.create(RetrofitObjectAPI.class);
Call<Weather> call = service.getWeatherDetails();
call.enqueue(new Callback<Weather>() {
@Override
public void onResponse(Response<Weather> response, Retrofit retrofit) {
try {
cityname.setText( response.body().getName());
temperature.setText(response.body().getTemp());
pressure.setText(response.body().getPressure());
humidity.setText(response.body().getHumidity());
wind.setText(response.body().getWind());
coord.setText(response.body().getCoord());
} catch (Exception e) {
Log.d("onResponse", "There is an error");
e.printStackTrace();
}
}
@Override
public void onFailure(Throwable t) {
Log.d("onFailure", t.toString());
}
});
}
}
I am not able to get data. Please help me as i am new to retrofit.
My Logcat
12-27 15:50:48.975 22956-22956/com.example.praveen.weather I/art:Late-enabling -Xcheck:jni
12-27 15:50:49.094 22956-22956/com.example.praveen.weather I/InstantRun: Instant Run Runtime started. Android package is com.example.praveen.weather, real application class is null.
12-27 15:50:49.129 22956-22956/com.example.praveen.weather W/art: Failed to find OatDexFile for DexFile /data/data/com.example.praveen.weather/files/instant-run/dex/slice-slice_6-classes.dex ( canonical path /data/data/com.example.praveen.weather/files/instant-run/dex/slice-slice_6-classes.dex) with checksum 0xb063b4c2 in OatFile /data/data/com.example.praveen.weather/cache/slice-slice_6-classes.dex
12-27 15:50:50.046 22956-22956/com.example.praveen.weather W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
12-27 15:50:50.551 22956-22956/com.example.praveen.weather I/ViewRootImpl: CPU Rendering VSync enable = true
12-27 15:50:50.554 22956-23012/com.example.praveen.weather D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
12-27 15:50:50.564 22956-22956/com.example.praveen.weather D/Atlas: Validating map...
12-27 15:50:50.606 22956-23012/com.example.praveen.weather I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_LA.BR.1.2.3_RB1.05.01.00.036.053_msm8909_LA.BR.1.2.3_RB1__release_AU (Iac7c2e2837)
OpenGL ES Shader Compiler Version: E031.25.03.04
Build Date: 09/01/15 Tue
Local Branch: mybranch13650421
Remote Branch: quic/LA.BR.1.2.3_rb1.76
Local Patches: NONE
Reconstruct Branch: AU_LINUX_ANDROID_LA.BR.1.2.3_RB1.05.01.00.036.053 + 7c4888a + 22ca218 + 855d166 + 95575a0 + 4193eef + 64d916f + 88f4cbe + c272012 + 046ce63 + 9b2bddc + NOTHING
12-27 15:50:50.607 22956-23012/com.example.praveen.weather I/OpenGLRenderer: Initialized EGL, version 1.4
12-27 15:50:50.616 22956-23012/com.example.praveen.weather D/OpenGLRenderer: Enabling debug mode 0
12-27 15:50:50.753 22956-22956/com.example.praveen.weather D/onFailure: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 11 path $.coord
12-27 15:50:50.753 22956-22956/com.example.praveen.weather I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@d717c04 time:40949345
There is no error.
wind is not a string, so Gson cannot cast it to String. You have to create its own object like what you did with Weather
Same for others. Hope it helps :)