1

I know a few people have asked this question before, but I have tried those answers. I am new to Android. Everything looks good, but I don't get why I am getting an empty object? Anyone can Guide me about that?

Interface:

public interface CryptoAPI {
@GET("ActivityForTestingPurposeOnly/")
io.reactivex.Observable<List<Stream>> getData(@Query("Row") String Row,
                                             @Query("Top") String Top,
                                             @Query("AppID") String appid);

This is my retrofit adapter:

public class RetrofitAdapter {
   private String BASE_URL = "http://m.ilmkidunya.com/api/sectionactivity/sectionactivity.asmx/"

   public static CryptoAPI newAPICreator() {
       final HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
       interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
       final OkHttpClient client = new OkHttpClient.Builder()
                                    .addInterceptor(interceptor)
                                    .build();

       Retrofit retrofit = new Retrofit.Builder()
             .client(client)            
             .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
             .addConverterFactory(GsonConverterFactory.create())
             .baseUrl(BASE_URL)
             .build();

            return retrofit.create(CryptoAPI.class);
        }
    }
}

Finally, the method where I am getting the response:

public void getStreams(){
  CryptoAPI.RetrofitAdapter.newAPICreator()
    .getData("0", "20", "73")
    .subscribeOn(Schedulers.io())
    .subscribeOn(AndroidSchedulers.mainThread())
    .subscribe(new Observer<Stream>() {
        @Override
        public void onSubscribe(Disposable d) {

        }

        @Override
        public void onNext(Stream model) {
                arrayList.add(model);                                  
                Toast.makeText(getApplicationContext(),
                               "Size: " + arrayList.size(),
                               Toast.LENGTH_SHORT);

        }
        @Override
        public void onError(Throwable e) {
            e.printStackTrace();
        }

        @Override
        public void onComplete() {

        }
    });
}

> here is my 



    @SerializedName("ID")
    @Expose
    private Integer iD;
    @SerializedName("Rating")
    @Expose
    private Integer rating;
    @SerializedName("SectionID")
    @Expose
    private Integer sectionID;
    @SerializedName("ContentID")
    @Expose
    private Integer contentID;

here you can see in the image

Irfan Yaqub
  • 140
  • 10
  • Have you tried hitting the api on Browser or Postman to check whether its really the issue of server? – nitinkumarp Jan 21 '19 at 09:04
  • 1
    `500-Internal Server Error` show that this error is from the server side, not your side. – Shubham Jan 21 '19 at 09:19
  • yeah i did, I am getting the response accurately. – Irfan Yaqub Jan 21 '19 at 09:25
  • solved that issue which was the wrong URL, now getting this error. – Irfan Yaqub Jan 21 '19 at 09:37
  • Which sounds like you're expecting an array `[ ... ]` but getting an Object `{ ... }`. Check the JSON you're receiving and adjust your code accordingly. – Michael Dodd Jan 21 '19 at 09:40
  • Or alternatively, please post an example of the JSON you're receiving from the request. – Michael Dodd Jan 21 '19 at 09:41
  • bro how i can check the Json i am receving? it is showing exception. when i change Array to object. error message is String resource ID #0x5 – Irfan Yaqub Jan 21 '19 at 09:45
  • `how i can check the Json i am receving?` By making the request in Postman, or dump to logcat [using this question](https://stackoverflow.com/questions/32965790/retrofit-2-0-how-to-print-the-full-json-response) – Michael Dodd Jan 21 '19 at 09:50

1 Answers1

1

Summary

I tested the endpoint you gave, and it's working fine

curl -X GET \
  'http://m.ilmkidunya.com/api/sectionactivity/sectionactivity.asmx/ActivityForTestingPurposeOnly?Row=0&Top=20&AppId=73' \
  -H 'Postman-Token: f64fdf34-4dbb-4201-93e9-e4c95fe7064d' \
  -H 'cache-control: no-cache'

The return is

{
    "stream": [
        {
            "ID": 583750,
            "Rating": 0,
            "SectionID": 59,
            "ContentID": 0,
            "SectionName": "Comments",
            "SortOrder": 2,
            "Title": "ICS",
            ...
        }
    ]
}

What went wrong

You have used Observable<List<DashboardDs>>, the List<T> class indicates that you are expecting an array as your ROOT json response

What you need to do

UPDATED (Based on the user's question update)

Create an object named Stream like this

public class Stream {
     @SerializedName("stream")
     List<StreamItem> streamItems; // <-- Array list of the stream items you have
}

Create an object named StreamItem like this

public class StreamItem {
     @SerializedName("ID") // <- json key
     Int id;
     @SerializedName("Rating")
     Int rating;
     @SerializedName("SectionID") 
     Int sectionId;
     @SerializedName("ContentID")
     Int contentId;
     @SerializedName("SectionName")
     String sectionName;
     @SerializedName("Title")
     String title;
     ... // additional properties you need
}

Then change your api service interface like this

io.reactivex.Observable<Stream>

In addition, if you're not using rxjava2 with the old rxjava1 or any related Observable class, you can just import the Observable class directly on the top most part of your service class

import io.reactivex.Observable

And use it like this

Observable<Stream>

Do it this way, use the Stream model object I provided above

public interface CryptoAPI {
@GET("ActivityForTestingPurposeOnly/")
Observable<Stream> getData(@Query("Row") String Row,
                                             @Query("Top") String Top,
                                             @Query("AppID") String appid);

This is how you call it

public void getStreams(){
  CryptoAPI.RetrofitAdapter.newAPICreator()
    .getData("0", "20", "73")
    .subscribeOn(Schedulers.io())
    .subscribeOn(AndroidSchedulers.mainThread())
    .subscribe(new Observer<Stream>() {
        @Override
        public void onSubscribe(Disposable d) {

        }

        @Override
        public void onNext(Stream stream) {
               // IMPORTANT NOTE:
               // Items return here in onNext does not mean the 
               // object you have (eg. each Stream's object in streams' list) 
               // it represents streams of generic data objects


               // This is where you can access the streams array
               arrayList.addAll(stream.streamItems) // <- notice the usage

               Toast.makeText(getApplicationContext(),
                               "Size: " + arrayList.size(),
                               Toast.LENGTH_SHORT);

        }
        @Override
        public void onError(Throwable e) {
            e.printStackTrace();
        }

        @Override
        public void onComplete() {

        }
    });
}

Read more on

Joshua de Guzman
  • 2,063
  • 9
  • 24