0

I have a news API JSON link which I want to convert to string array so that I can use them in recycled view.

I am a new to Java, kindly help me to know the way to do it.

I am trying to use Android Networking Library and GSON for this purpose.

package in.apptonic.lalit.newsapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.androidnetworking.AndroidNetworking;
import com.androidnetworking.error.ANError;
import com.androidnetworking.interfaces.JSONArrayRequestListener;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import in.apptonic.lalit.newsapplication.model.News;

public class MainActivity extends AppCompatActivity {

    RecyclerView recyclerView;
    RecyclerView.LayoutManager layoutManager;
    RecyclerView.Adapter adapter;
    List<News> newsList = new ArrayList<>();
    private Gson gson;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        GsonBuilder gsonbuilder = new GsonBuilder();
        gsonbuilder.setDateFormat("M/d/yy hh:mm a");
        gson = gsonbuilder.create();

        AndroidNetworking.initialize(getApplicationContext());

        recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new AdapterNews(newsList);
        recyclerView.setAdapter(adapter);
        downloadNews();
    }

    private void downloadNews() {

        AndroidNetworking.get("https://newsapi.org/v1/articles?source=the-next-web&sortBy=latest&apiKey=hidden")
                .build()
                .getAsJSONArray(new JSONArrayRequestListener() {
                    @Override
                    public void onResponse(JSONArray response) {

                        List<News> news = Arrays.asList(gson.fromJson(String.valueOf(response), News[].class));

                        //TODO How to convert JSON values n String so that I can send them in adapter for recycler view
                    }

                    @Override
                    public void onError(ANError anError) {


                    }
                });


    }
}

2 Answers 2

2

First create a list:

List<String> list = new ArrayList<String>();

Then you can add the string values from the JSONArray to the list like this:

for (int i = 0; i < response.length(); i++) {
    list.add(response.getJSONObject(i).getString("name"));
}

Then pass this list variable to your adapter.

Sign up to request clarification or add additional context in comments.

Comments

2

Create a method inside your AdapterNews class something like this

public void updateNewsList(List<News> list){
    this.newsList = list;  // newsList is the list which holds news data inside your adapter.
    notifyDataSetChanged();
}

Call this method in onResponse of your network request.

AndroidNetworking.get("https://newsapi.org/v1/articles?source=the-next-web&sortBy=latest&apiKey=hidden")
            .build()
            .getAsJSONArray(new JSONArrayRequestListener() {
                @Override
                public void onResponse(JSONArray response) {

                    List<News> news = Arrays.asList(gson.fromJson(String.valueOf(response), News[].class));
                    adapter.updateNewsList(news);
                }

                @Override
                public void onError(ANError anError) {
                }
            });

1 Comment

Just an additional information. This is my Json response. newsapi.org/v1/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.