Deepak Rattan
Deepak Rattan

Reputation: 1299

Getting Null Response using Retrofit Library in android

I am using Retrofit Library in my app for network requests.My code is given below:

1. MyAPI.java(Interface)

    public interface MyAPI {
    @GET("/{roomID}")
    Call<Messages>loadMessages(@Path("roomID") String roomID);
}

2. Message.java

    public class Message {

    String user;
    String message;
    //String roomID;

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    @Override
    public String toString() {
        return(message);
    }
}

3.Messages.java

    public class Messages {
    List<Message> items;
}

4.MainActivity.java

public class MainActivity extends AppCompatActivity {
String roomID = "548b737c0eadfb00eb93891bb28242e5";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("http://pms.vebific.com:81/chat/index/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    MyAPI myAPI = retrofit.create(MyAPI.class);
    Call<Messages> call = myAPI.loadMessages(roomID);

    call.enqueue(new Callback<Messages>() {
        @Override
        public void onResponse(Response<Messages> response, Retrofit retrofit) {
            Log.e("Response", String.valueOf(response.body()));
        }

        @Override
        public void onFailure(Throwable t) {
            Log.e("Error","---------------");
        }
    });

}

}

URL to be used is :

http://pms.vebific.com:81/chat/index/548b737c0eadfb00eb93891bb28242e5

Expected Response:

 [
  {
"_id": {
"$id": "56963d96da036c6c05815e63"
},
"room_id": "548b737c0eadfb00eb93891bb28242e5",
"user": "Jeevan Verma",
"id": 479,
"message": "\\uD83D\\uDC30",
"date": 1481587559734,
"status": "sent"
},
{
"_id": {
"$id": "56963d8cda036c6c05815e62"
},
"room_id": "548b737c0eadfb00eb93891bb28242e5",
"user": "Jeevan Verma",
"id": 479,
"message": "\\uD83D\\uDE0A",
"date": 1481587559734,
"status": "sent"
},
{
"_id": {
"$id": "56964454da036c6c05815e64"
},
"room_id": "548b737c0eadfb00eb93891bb28242e5",
"user": "Jeevan Verma",
"id": 479,
"message": "\\uD83D\\uDC2D",
"date": 1481589296434,
"status": "sent"
}
]

When i am using it in the browser,i am getting response.

While using Retrofit ,i am getting null response .I might be missing something ,i guess.Please help me to fix it. Please tell me how to read response using Retrofit.

Upvotes: 0

Views: 617

Answers (1)

H&#233;ctor
H&#233;ctor

Reputation: 26034

Change your api definition to return Call<List<Message>> instead of Call<Messages>:

Call<List<Message>>loadMessages(@Path("roomID") String roomID);

and

MyAPI myAPI = retrofit.create(MyAPI.class);
    Call<List<Message>> call = myAPI.loadMessages(roomID);

    call.enqueue(new Callback<List<Message>>() {
        @Override
        public void onResponse(Response<List<Message>> response, Retrofit retrofit) {
            Log.e("Response", String.valueOf(response.body()));
        }

        @Override
        public void onFailure(Throwable t) {
            Log.e("Error","---------------");
        }
    });

Upvotes: 2

Related Questions