to retrieve data using retrofit in android

3.5k views Asked by At

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.

3

There are 3 answers

2
Nhan On BEST ANSWER

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

class Wind {
   float speed;
   int deg;
   public Wind(float speed, int deg){this.speed = speed; this.deg = deg;}
   public float getSpeed(){return speed;}
   public void setSpeed(float speed){this.speed = speed;}
   public int getDeg(){return deg;}
   public void setDeg(int deg){this.deg = deg;}
}
// Then use this in Weather class
Wind wind; // Instead of String wind
// Whenever you want to get wind speed with your response from server
float windSpeed = response.getWind().getSpeed();

Same for others. Hope it helps :)

1
RadekJ On

what do you expecting to have in String wind; in your response if it is:

 "wind": {
      "speed": 3.6,
      "deg": 60
 },

you should write class 'Wind' and in it you would have two fields:

double speed;
int deg;

Then in your Weather class field named 'wind' would be of type Wind instead of String.

1
saipok On

Here is the method:

etSearch.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) {

        }

        @Override
        public void onTextChanged(final CharSequence s, final int start, final int before, final int count) {

        }

        @Override
        public void afterTextChanged(final Editable s) {
            if (s.length() == 0) {
                setListData(notesViewslist);
            } else if (s.length() > 0) {
                performLocalSearch();
            }
        }
    });
}

private void  performLocalSearch() {
    if (etSearch.getText().toString().trim().length() == 0) {
        setListData(notesViewslist);
    } else {
        final String search = etSearch.getText().toString().trim().toLowerCase();
        final ArrayList<NotesViews> mSearchList = new ArrayList<NotesViews>();
        for (NotesViews mServices : notesViewslist) {
            if (mServices.getTitle() != null && mServices.getTitle().toLowerCase().contains(search)) {
                mSearchList.add(mServices);
            }
            else if (mServices.getCreateddate() != null && mServices.getCreateddate().toLowerCase().contains(search))
            {
                mSearchList.add(mServices);
            }
        }
        setListData(mSearchList);
    }
}