Raymond Truong
Raymond Truong

Reputation: 1

Java API for yelp, illegalStateExceptom

I want to retrieve data but API isn't letting me, onFailure runs and I have no idea why, please see code below. I believe there is something wrong with my dataRespone class but I'm not sure what. I have tried to trouble shoot and used chatgpt. Also compared it to my instructors code and I can't spot a difference and I have implemented all dependancies. If you need more code please let me know.

Sorry if this is a silly question, like I said, new to coding :)

 public class dataResponse implements Serializable {

    @SerializedName("total")
    int total;

    @SerializedName("businesses")
    ArrayList<Business> businesses;


    class Business {

        @SerializedName("rating")
        int rating;

        @SerializedName("price")
        String price;

        @SerializedName("categories")
        ArrayList<Category> categories;

        @SerializedName("location")
        Location location;
    }

    class Category {

        @SerializedName("alias")
        String alias;

        @SerializedName("title")
        String title;
    }

    class Location {

        @SerializedName("city")
        String city;

        @SerializedName("zip_code")
        String zip_code;

    }

{
  "businesses": [
    {
      "alias": "golden-boy-pizza-hamburg",
      "categories": [
        {
          "alias": "pizza",
          "title": "Pizza"
        },
        {
          "alias": "food",
          "title": "Food"
        }
      ],
      "coordinates": {
        "latitude": 41.7873382568359,
        "longitude": -123.051551818848
      },
      "display_phone": "(415) 982-9738",
      "distance": 4992.437696561071,
      "id": "QPOI0dYeAl3U8iPM_IYWnA",
      "image_url": "https://yelp-photos.yelpcorp.com/bphoto/b0mx7p6x9Z1ivb8yzaU3dg/o.jpg",
      "is_closed": true,
      "location": {
        "address1": "James",
        "address2": "Street",
        "address3": "68M",
        "city": "Los Angeles",
        "country": "US",
        "display_address": [
          "James",
          "Street",
          "68M",
          "Los Angeles, CA 22399"
        ],
        "state": "CA",
        "zip_code": "22399"
      },
      "name": "Golden Boy Pizza",
      "phone": "+14159829738",
      "price": "$",
      "rating": 4,
      "review_count": 903,
      "transactions": [
        "restaurant_reservation"
      ],
      "url": "https://www.yelp.com/biz/golden-boy-pizza-hamburg?adjust_creative=XsIsNkqpLmHqfJ51zfRn3A&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=XsIsNkqpLmHqfJ51zfRn3A"
    }
  ],
  "region": {
    "center": {
      "latitude": 37.76089938976322,
      "longitude": -122.43644714355469
    }
  },
  "total": 6800
}


public class MainActivity extends AppCompatActivity {

    SearchView searchView;
    String auth = "Bearer p8zuza3LuAcpUQfF6TpOoKs9bnoQWiv1sPLUa75RfghOHPW_srLe5vdJlj-ma4Em5-wgd3nQ2f9UMP-UH5uhZHAHu8RNVyfZLPTOzooTGSNm0ioC4zzmrIf5VyaJZHYx";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String location = "Montreal";
        String term = "food";

        Call<List<dataResponse>> call = RetrofitClient.getInstance().getApi().getBusinesses(location, term);

        call.enqueue(new Callback<List<dataResponse>>() {
            @Override
            public void onResponse(Call<List<dataResponse>> call, Response<List<dataResponse>> response) {
                    List<dataResponse> data = response.body();

            }

            @Override
            public void onFailure(Call<List<dataResponse>> call, Throwable t) {
                Toast.makeText(MainActivity.this, "Request Failed", Toast.LENGTH_SHORT).show();
                Log.e("TAG", "Request Failed: " + t.getMessage(), t);
            }
        });
    }

}

**logcat**

Request Failed: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
at com.google.gson.stream.JsonReader.beginArray(JsonReader.java:358)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:80)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:40)
at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:27)
at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:243)
at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:153) at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:539)at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1137)atjava.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:637)at java.lang.Thread.run(Thread.java:1012)

Upvotes: 0

Views: 50

Answers (0)

Related Questions