I am trying to retrieve data from MySQL using Retrofit.
I found nothing error in my Code but when I run the application from device it stucks on Progress Dialog Fetching Data...
Below are my codes for fetching the data
private void getBooks(){
final ProgressDialog loading = ProgressDialog.show(this,"Mengambil Data..","Silahkan Tunggu...",false,false);
RestAdapter adapter = new RestAdapter.Builder()
.setEndpoint(ROOT_URL)
.build();
BookAPI api = adapter.create(BookAPI.class);
api.getBooks(new Callback<List<Book>>() {
@Override
public void success(List<Book> list, Response response) {
loading.dismiss();
books = list;
showList();
}
@Override
public void failure(RetrofitError error) {
}
});
}
private void showList(){
String[] items = new String[books.size()];
for(int i = 0; i<books.size();i++){
items[i] = books.get(i).getName();
}
ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.simple_list,items);
listView.setAdapter(adapter);
}
and also below the Interface I have
public interface BookAPI {
@GET("/retrofit/listdata.php")
public void getBooks(Callback<List<Book>> response);
}
and finally my php file for listing data named listdata.php
$link = mysqli_connect('localhost', 'root', '') or die('Cannot connect to the DB');
mysqli_select_db($link, 'retrofit') or die('Cannot select the DB');
$query = "SELECT * FROM tbl_book";
$rs = mysqli_query($link, $query) or die('Errorquery: ' . $query);
$countrow = mysqli_affected_rows($link);
$items = array();
while($row = mysqli_fetch_object($rs)) {
array_push($items, $row);
}
if ($countrow > 0) {
// $result["errorcode"] = "1";
$result = $items;
} else {
$result["errormsg"] = "Tidak ada data";
}
echo json_encode($result);
Is it because I use the php file instead of json file on my Interface?
Source: View source