1
votes

I am using Retrofit in my app to read data from JSON to my app. I have this JSON. (I have tried many solution but nothing worked for me).Please Help me with That. i have used the same code code with other JSON and it works with this This JSON URL but it is not Working with my JSON

JSON My JSON is placed at this URL

My Interface:

    public interface SanaApi {

    String BASE_URL = "http://www.hhfoodies.com/salesnotifier/";

    @GET("sana_safinaz.php")
    Call<List<SanaSafinas>> getSana();
}

Here is Model Class:

  public class SanaSafinas {

    private String id;
    private String off;
    private String image;
    private String title;
    private String sale_price;
    private String original_price;

    public SanaSafinas(String id, String off, String image, String title, String sale_price, String original_price) {
        this.id = id;
        this.off = off;
        this.image = image;
        this.title = title;
        this.sale_price = sale_price;
        this.original_price = original_price;
    }

    public String getId() {
        return id;
    }

    public String getOff() {
        return off;
    }

    public String getImage() {
        return image;
    }

    public String getTitle() {
        return title;
    }

    public String getSale_price() {
        return sale_price;
    }

    public String getOriginal_price() {
        return original_price;
    }
}

Here is Main Activity:

 public class MainActivity extends AppCompatActivity {

    ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = (ListView) findViewById(R.id.listViewHeroes);

        //calling the method to display the heroes
        getSana();
    }

    private void getSana() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(SanaApi.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create()) //Here we are using the GsonConverterFactory to directly convert json data to object
                .build();

        SanaApi api = retrofit.create(SanaApi.class);

        Call<List<SanaSafinas>> call = api.getSana();

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

                //Creating an String array for the ListView
                String[] heroes = new String[heroList.size()];

                //looping through all the heroes and inserting the names inside the string array
                for (int i = 0; i < heroList.size(); i++) {
                    heroes[i] = heroList.get(i).getSale_price();
                }


                //displaying the string array into listview
                listView.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, heroes));

            }



            @Override
            public void onFailure(Call<List<SanaSafinas>> call, Throwable t) {
                Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }


}
2
Your model class is not created properly as if you see the response all the data is coming inside the data JsonArray and you are directly doing your work over there, change your model class to make it work properly - Rakshit Nawani
Copy all your response in jsonschema2pojo.org and it will create your Model class automatically target lang: java Source type: JSON Annotation style: Gson and check include getter and setter - Rakshit Nawani

2 Answers

1
votes

Your retrofit call expects a List<SanaSafinas>, which is generated from a JSON array.

Your JSON is not an array, it is an object and that's what you need to tell retrofit that you expect.

class Response {
  private final List<SanaSafinas> data;

  public Response(List<SanaSafinas> data) {
    this.data = data;
  }

  // Getter...
}

And then your interface will look like:

public interface SanaApi {

String BASE_URL = "http://www.hhfoodies.com/salesnotifier/";

@GET("sana_safinaz.php")
Call<Response> getSana();

JSON Objects are enclosed in curly braces like yours:

{
  "message": "",
  "status": 1,
  "data": [
  {
    "id": "s18220052",
    "off": "50% Off",
    "image": "https://www.sanasafinaz.com/media/catalog/product/cache/2cf0e7f44e364cdf6f00ee795133d228/s/1/s18220052_1_.jpg",
    "title": "SAFARI-O",
    "sale_price": "PKR 2,720.00",
    "original_price": "PKR 5440 "
  },
...
}

JSON arrays are enclosed in brackets, like your first url:

[
  {
    "name": "Captain America",
    "realname": "Steve Rogers",
    "team": "Avengers",
   "firstappearance": "1941",
   ....
  },
  ...
}
0
votes

Your model class is not created properly as if you see the response all the data is coming inside the data JsonArray and you are directly doing your work over there, change your model class to make it work properly

Create a new Class which will be containing your response lets name it ResponseSana

public class ResponseSana {

@SerializedName("message")
@Expose
private String message;
@SerializedName("status")
@Expose
private Integer status;
@SerializedName("data")
@Expose
private List< SanaSafinas > data = null;

public String getMessage() {
return message;
}

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

public Integer getStatus() {
return status;
}

public void setStatus(Integer status) {
this.status = status;
}

public List< SanaSafinas > getData() {
return data;
}

public void setData(List< SanaSafinas > data) {
this.data = data;
}

}

and your interface should be like this

Call<ResponseSana> getSana();

and then change your onResponse accordingly to the changed code and all the data will be working fine with no error.