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();
}
});
}
}