• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

How to Populate a Spinner from Database using Retrofit2

redmoonx

Lurker
Hello,

I've been searching around for ways to populate a spinner using Retrofit2 to fetch the data from a mysql database and I haven't had much luck.

I've tried different approaches and none has been specific to retrofit and I got the point that I deleted everything I had worked on to start from scratch, which is why I am now resorting to ask for help in this forum.

What I want to do is to fetch a json array from the database and pass it to the spinner.

Everything I deleted was what I had worked on for the Spinner but the basics of my project are still there.

This is the retrofit code I am using.

Config class
Code:
package com.test.test.network.config;
public class Config {

    public static final String BASE_URL = "http://xxx.xxx.xx.xx";
    public static final String API_URL = BASE_URL + "/testapp";
    public static final String API_COUNTRY_LIST = API_URL + "/country_list.php";

}

Retrofit Builder Class
Code:
package com.test.test.network.config;

import com.test.test.BuildConfig;
import java.util.concurrent.TimeUnit;
import android.content.Context;

import okhttp3.Cache;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitBuilder {

    public static Retrofit builder(Context context) {
        OkHttpClient.Builder okhttpBuilder = new OkHttpClient().newBuilder();
        okhttpBuilder.connectTimeout(120, TimeUnit.SECONDS);
        okhttpBuilder.writeTimeout(60, TimeUnit.SECONDS);
        okhttpBuilder.readTimeout(60, TimeUnit.SECONDS);

        int cacheSize = 10 * 1024 * 1024;
        Cache cache = new Cache(context.getCacheDir(), cacheSize);
        okhttpBuilder.cache(cache);

        if (BuildConfig.DEBUG) {
            HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
            interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            okhttpBuilder.addInterceptor(interceptor);
        }

            retrofit = new Retrofit.Builder()
                    .baseUrl(Config.BASE_URL)
                    .client(okhttpBuilder.build())
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        return retrofit;
    }
}


If there is anything else needed from my part and I have the code available. I'll gladly update with the needed information. I just need someone to help me out and show me how to populate a spinner from mysql db using retrofit.

Thanks in advance.
 
I've not used Retrofit before, but from a quick search it looks like a library to ease the pain of retrieving JSON data from a remote server. Basically it contains mechanisms to map the JSON elements into a Java model class.
I scanned over this tutorial

http://www.vogella.com/tutorials/Retrofit/article.html

What's missing in your code fragments above are the details of how you're converting the received JSON data into your Java model class. I presume you have code to do that?
 
Back
Top Bottom