Shivam Kapoor
Shivam Kapoor

Reputation: 127

How to pass json array using get method in retrofit android

I am new to retrofit,and not having much idea how to pass this json using retrofit and show in my app.Below is my json, MainActivity and model class.But know it is throwing an exception "Except begin_object but found begin_array".Please help me.

[
    {
        "userId": 1,
        "id": 1,
        "title": "sunt",
        "body": "sdfdsf"
      },
      {
        "userId": 1,
        "id": 2,
        "title": "qui est esse",
        "body": "jhmjk"
      }]

MainActivity.java

Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ApiInterface apiService = retrofit.create(ApiInterface.class);
        // ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
        Call<ModelClass> call = apiService.getLogin();
        // Call<LoginResponse> call = apiService.loginWithCredentials(new LoginRequest(email_enter, md5pass));
        call.enqueue(new Callback<ModelClass>() {
            @Override
            public void onResponse(Call<ModelClass> call, Response<ModelClass> response) {
                Log.i("REGISTRATION --->", "Registered" + response);
            }

            @Override
            public void onFailure(Call<ModelClass> call, Throwable t) {
                Log.i("REGISTRATION --->", "Throwable" + t.toString());
                // Intent i = new Intent(MainActivity.this,SecondActivity.class);
                // startActivity(i);
            }
        });
    }

ModelClass.java

public class ModelClass {
    class Model{
        public List<ObjectModel> getObjectmodel() {
            return objectmodel;
        }

        public void setObjectmodel(List<ObjectModel> objectmodel) {
            this.objectmodel = objectmodel;
        }

        List<ObjectModel> objectmodel;
    }

    class ObjectModel{
        String userId;
        String id;

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getUserId() {
            return userId;
        }

        public void setUserId(String userId) {
            this.userId = userId;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getBody() {
            return body;
        }

        public void setBody(String body) {
            this.body = body;
        }

        String title;
        String body;
    }
}

Upvotes: 0

Views: 949

Answers (2)

Ragini
Ragini

Reputation: 332

To pass json request, you have to use post method.

Upvotes: 0

Jd Prajapati
Jd Prajapati

Reputation: 1971

Create custom model and pass same model in retrofit api.

class Model{
   List<ObjectModel> objectmodel;
 }

class ObjectModel{
   String userId;
   String id;
   String title;
   String body;
}

Bind value and pass Model object in your retrofit interface.

Or

@GET("json")
Call<JsonObject> youmethode(@Query("key") String value);

Hope this is helpful. thanks

Upvotes: 2

Related Questions