PHP file stored in www directory of WAMP
<?php
//Response array JSON
$response = array();
//Connexion to the db local in WAMP
include("connexion_db.php");
$idcom = connexobject("database_app_android","myparam");
$sql = "SELECT * FROM products";
$result = $idcom->query($sql);
if(!$result)
{
//Cannot read
$response["success"] = 0;
$response["mesage"] = "SQL query has timed out";
}
else
{
if($result->num_rows > 0)
{
//Products founds
$response["products"] = array();
while($row = $result->fecth_object())
{
$product = array();
foreach ($row as $key => $value)
{
$product[$key] = $value;
}
//Add product to the response JSON array
array_push($response["products"], $product);
}
$response["success"] = 1;
}
else
{
//0 product found
$response["success"] = 0;
$response["mesage"] = "No products found";
}
}
//Encode and send response in JSON format
echo(json_encode($response));
$response->free();
$result->free();
$idcom->close();
?>
Android API
public interface CatalogAPI {
@GET("/products")
Call<List<Product>> getProductList();
@GET("/products/{product}")
Call<Product> getProduct(@Path("product") int productId);
}
Android part using retrofit
//Retrofit
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://10.0.2.2:3000/MyAndroidApp/get_all_product.php")
.addConverterFactory(GsonConverterFactory.create())
.build();
CatalogAPI service = retrofit.create(CatalogAPI.class);
service.getProductList().enqueue(new Callback<List<Product>>() {
@Override
public void onResponse(Call<List<Product>> call, Response<List<Product>> response) {
Log.i("retrofit","DOWNLOAD OK");
for (Product p : response.body())
{
Log.i("retrofit","Product : " + p.getName());
catalog.add(p);
}
}
@Override
public void onFailure(Call<List<Product>> call, Throwable t) {
}
});
Please, why this doesn't work ? I use the Android studio emulator. The php script encode to json all products founds in the mysql database. The MySQL database is on a local server wamp
Please add logging interceptor to your retrofit client, this would help to find where is the problem .
Gradle Change
compile 'com.squareup.okhttp3:logging-interceptor:3.3.1'
Code Change
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
// set your desired log level
logging.setLevel(Level.BODY);
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
// add your other interceptors …
// add logging as last interceptor
httpClient.addInterceptor(logging); // <-- this is the important line!
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API_BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient.build())
.build();
In php file write
while($row = $result->**fetch**_object())
instead of
while($row = $result->**fecth**_object())
Good day, i know this is coming late though, but the problem seems to come from the way PHP encodes into json array. PHP adds extra features like " " and more to encode into json array, which is totally different from what Retrofit 2 is expecting. Retrofit 2 expects something like:
[
{ "name": "John Doe", "address": "60, station road", "mobile": "1" },
{ "name": "Doe Chujwu", "address": "6, Morgan Estate", "mobile": "2" }
].
Hence change your output to echo the above give format and then would your code work. I hope it helps. NB: also test that you can access your server homepage on the emulators browser. (This is to ensure that your are using the right ipaddress to connect).