Retrofit Expected BEGIN_OBJECT but was BEGIN_ARRAY

I’m fairly new to JSON parsing , I’m using the Retrofit library of Square and ran into this problem.

I’m trying to parse this JSON reponse:

  • Setting up Android support package v7 for eclipse - GridLayout
  • What is the new generated code “This was auto-generated to implement the App Indexing API.”?
  • Make webview in a tablet simulate a small device
  • unable to implement descriptors in android
  • Volley JsonObjectRequest Post request not working
  • Apache HTTP Client Removal from API23, will that have an effect on Volley?
  • [
          {
            "id": 3,
            "username": "jezer",
            "regid": "oiqwueoiwqueoiwqueoiwq",
            "url": "http:\/\/192.168.63.175:3000\/users\/3.json"
          },
          {
            "id": 4,
            "username": "emulator",
            "regid": "qwoiuewqoiueoiwqueoq",
            "url": "http:\/\/192.168.63.175:3000\/users\/4.json"
          },
          {
            "id": 7,
            "username": "test",
            "regid": "ksadqowueqiaksj",
            "url": "http:\/\/192.168.63.175:3000\/users\/7.json"
          }
    ]
    

    Here are my models:

    public class Contacts {
    
        public List<User> contacts;
    
    }
    

    public class User {
    
        String username;
        String regid;
    
        @Override
        public String toString(){
            return(username);
        }  
    
    }
    

    my Interface:

    public interface ContactsInterface {
    
        @GET("/users.json")
        void contacts(Callback<Contacts> cb);
    
    }
    

    my success method:

    @Override
    public void success(Contacts c, Response r) {
        List<String> names = new ArrayList<String>();
        for (int i = 0; i < c.contacts.size(); i++) {
            String name = c.contacts.get(i).toString();
            Log.d("Names", "" + name);
            names.add(name);
        }
        ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, names);
        mSentTo.setAdapter(spinnerAdapter);
    }
    

    When I use it on my success method it throws the error

    Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column2

    What is wrong here?

  • android: turn off screen when close to face
  • How do I remove extra space above and below imageView?
  • How do I set a different color for the pressed state of the button?
  • Could not determine the dependencies of task ':app:crashlyticsStoreDeobsDebug' if I enable the proguard
  • What size of screenshot should I use in the Android Market?
  • Loading kernel module in Android kernel
  • 4 Solutions collect form web for “Retrofit Expected BEGIN_OBJECT but was BEGIN_ARRAY”

    Right now you are parsing the response as if it was formatted like this:

    {
      "contacts": [
        { .. }
      ]
    }
    

    The exception tells you this in that you are expecting an object at the root but the real data is actually an array. This means you need to change the type to be an array.

    The easiest way is to just use a list as the direct type in the callback:

    @GET("/users.json")
    void contacts(Callback<List<User>> cb);
    

    in your interface
    replace

    @GET("/users.json")
    void contacts(Callback<Contacts> cb);
    

    By this code

    @GET("/users.json")
    void contacts(Callback<List<Contacts>> cb);
    

    Convert it into a list.

    Below is the example:

    BenchmarksListModel_v1[] benchmarksListModel = res.getBody().as(BenchmarksListModel_v1[].class);
    

    Source Code Working

    https://drive.google.com/open?id=0BzBKpZ4nzNzUVFRnVVkzc0JabUU

    public interface ApiInterface {
        @GET("inbox.json")
        Call<List<Message>> getInbox();
    }
    
     call.enqueue(new Callback<List<Message>>() {
                @Override
                public void onResponse(Call<List<Message>> call, Response<List<Message>> response) {
    
            YourpojoClass.addAll(response.body());
    
                    mAdapter.notifyDataSetChanged();
                }
    
                @Override
                public void onFailure(Call<List<Message>> call, Throwable t) {
                    Toast.makeText(getApplicationContext(), "Unable to fetch json: " + t.getMessage(), Toast.LENGTH_LONG).show();
                }
            });
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.