Consuming an API via Retrofit library in Android

Issue

I’m having an issue with implementing retrofit library on my Android Studio project. I have an API made on Ruby on Rails and the android app calling it through HTTP methods. The problem is, when I open the app it shuts down showing the error “Unfortunately, the app has stopped”
Since the code is showing no errors, I believe it’s about the component’s versions in Gradle
This is the gradle code

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion '25.0.1'
    defaultConfig {
        applicationId "com.example.leandro.retrofit"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
    compile 'com.squareup.retrofit2:retrofit:2.4.0'
    compile 'com.squareup.retrofit2:converter-gson:2.4.0'
    compile 'com.android.support:recyclerview-v7:25.3.1'
}

I will leave the code of the app below, just in case

ArticleActivity Java Class

public class ArticleActivity extends AppCompatActivity {


    private final String baseUrl = "http://localhost:3000/";
    List<Article> listaArticles = new ArrayList<>();
    ArticleAdapter adapter;
    RecyclerView rvClientes;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_article);

        Retrofit retrofit = new Retrofit.Builder().baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        LinearLayoutManager llm = new LinearLayoutManager(getApplicationContext());
        rvClientes.setLayoutManager(llm);

        adapter =  new ArticleAdapter(listaArticles);
        rvClientes.setAdapter(adapter);

        DividerItemDecoration itemDecoration = new DividerItemDecoration(rvClientes.getContext(),llm.getOrientation());
        rvClientes.addItemDecoration(itemDecoration);

        ArticleService articleService = retrofit.create(ArticleService.class);

        Call<List<Article>> lista = articleService.getArticles();
        lista.enqueue(new Callback<List<Article>>() {
            @Override
            public void onResponse(Call<List<Article>> call, Response<List<Article>> response) {
                if(response.isSuccessful()) {
                listaArticles = response.body();
                adapter = new ArticleAdapter(listaArticles);
                rvClientes.setAdapter(adapter);
                adapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(Call<List<Article>> call, Throwable t) {

            }
        });

    }
}

ArticleAdapter

public class ArticleAdapter extends RecyclerView.Adapter<ArticleAdapter.ArticleViewHolder> {

    List<Article> lista;


    public ArticleAdapter(List<Article> lista) {
        this.lista = lista;
    }

    @Override
    public ArticleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.article_row,parent,false);
        return new ArticleViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ArticleViewHolder holder, int position) {
        holder.bindArticle(lista.get(position));
    }

    @Override
    public int getItemCount() {
        return lista.size();
    }

    public class ArticleViewHolder extends RecyclerView.ViewHolder {

        TextView tvNombre;
        TextView tvId;



        public ArticleViewHolder(View itemView) {
            super(itemView);
            tvNombre = (TextView) itemView.findViewById(R.id.tvArticle);
            tvId = (TextView) itemView.findViewById(R.id.tvID);

        }

        public void bindArticle(Article article) {
            String nombre = article.getTitle() + ":";
            tvNombre.setText(nombre);
            tvId.setText(String.valueOf(article.getId()));
        }

    }
}

Solution

You’re setting RecyclerView adapter without initializing RecyclerView object rvClientes.

Just initialize RecyclerView object after setContentView() method inside onCreate()

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_article);

    rvClientes = (RecyclerView) findViewById(R.id.RV_ID_DEFINED_IN_LAYOUT_XML_FILE);  // change this ID

    // rest of the code
}

Change RecyclerView ID in findViewById()

Answered By – Shashanth

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published