-3

I am new to Retrofit and Android, and now i am stuck at receiving the JSON from API.

I want full solution to parse this JSON and populate it into RecyclerView or ListView.

The Sample of JSON Data is below:

["C","Python","Swift","Ruby","Javascript","PHP","C#","Java"]
Apoorv Mehrotra
  • 607
  • 5
  • 13
Hemraj Rijal
  • 65
  • 1
  • 2
  • 12

3 Answers3

1

Add this to your Retrofit interface:

@GET("api/programming_languages")
Call<List<String>> getLanguagesList();

And use it like this:

Call<List<String>> languagesCall = service.getLanguagesList();
languagesCall.enqueue(new Callback<List<String>>() {
    @Override
    public void onResponse(Response<List<String>> response) {
        List<String> languages = response.body();
    }

    @Override
    public void onFailure(Throwable t) {

    }
});

There are many other tutorials for Retrofit 2.0 so go and find something.

reaven
  • 91
  • 4
  • languagesCall.enqueue(new Callback() what should I use on , I don't have model of this json. I was unable to make a model class using [link] (http://www.jsonschema2pojo.org/) . URL for this JSON is : http://api.technorio.com/tags I am waiting for your help @reaven – Hemraj Rijal Feb 03 '17 at 12:12
  • It should be "List" as well. I edited my answer. – reaven Feb 03 '17 at 12:53
  • Yes it parse the String but how do I populate this into RecyclerView? @Override public void onBindViewHolder(TagAdapter.Tag_ViewHolder holder, final int position) { holder.tagName.setText(Tags.get(position). How do I Get That Value); } – Hemraj Rijal Feb 03 '17 at 16:49
0

They have the full documentation here https://square.github.io/retrofit/ Also you cannot have a json with ["", "", ""] format. What you can have is a format like [{"language":"C"},{"language":"Python"}.... etc ] and parse them using a CustomAdapter for List/RecyclerView. Using an ArrayList or ArrayList

Mike
  • 1,313
  • 12
  • 21
  • Yes, there exist ["", ""] type of JSON and can be parsed using retrofit. You can look into this http://stackoverflow.com/questions/28628513/how-to-parse-json-array-without-any-object-in-retrofit – Hemraj Rijal Feb 03 '17 at 05:43
  • Its a simple string array, you cannot have that format of response if you want to map your values out, if not then feel free to use it. – Mike Feb 03 '17 at 06:12
0

For eg your api is: @GET("/programming_languages") Call> groupList();

Then after calling the groupList() method you should get the response as expected.

Sagar Pujari
  • 343
  • 1
  • 11
  • Can you please tell me more on detail. now I am Using @GET("/tags") Call tags(); – Hemraj Rijal Feb 03 '17 at 05:54
  • check the code Call call = groupList(); call.enqueue(new Callback() { public void onResponse(Callcall, Response response) { Log.d(TAG, response.body().toString()); } public void onFailure(Call call, Throwable t) { } }); – Sagar Pujari Feb 03 '17 at 06:17
  • java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hemraj.technorio_posts/com.example.hemraj.technorio_posts.activity.TagActivity}: java.lang.IllegalArgumentException: Unable to create call adapter for interface retrofit2.Call for method ApiInterface.groupList – Hemraj Rijal Feb 03 '17 at 12:22
  • I think Call is incorrect, try Call instead. – Sagar Pujari Feb 03 '17 at 12:41