PHP code :(DBadapter.php)
<?php
require_once 'init.php';
class DBadapter
{
public function select()
{
global $con;
$retrived = $con->query("select id,name,quantity from product");
echo"in DBadapter";
if ($retrived) {
while ($row=mysqli_fetch_array($retrived)) {
$product[]=$row;
}
print(json_encode($product));
}
else
{
print(json_encode(array("CANT retrive data")));
}
}
}
?>
(index.php):
<?php
require_once 'DBadapter.php';
$dbadapter=new DBadapter();
$dbadapter->select();
?>
Android code:(The catch block here is being executed here)
here the data from the table "products" is fetched from the phpMysql database and retrived in Tableview in android studio
package edmt.dev.androidgridlayout.mMySQL;
import android.content.Context;
import android.widget.Toast;
import com.androidnetworking.AndroidNetworking;
import com.androidnetworking.common.Priority;
import com.androidnetworking.error.ANError;
import com.androidnetworking.interfaces.JSONArrayRequestListener;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import de.codecrafters.tableview.TableView;
import de.codecrafters.tableview.toolkit.SimpleTableDataAdapter;
import edmt.dev.androidgridlayout.TableHelper;
import edmt.dev.androidgridlayout.mModel.Product;
public class MySQLClient {
private static final String retrive_url="http://10.0.2.2/kamakshi/index.php";
private final Context c;
private SimpleTableDataAdapter adapter;
public MySQLClient(Context c) {
this.c = c;
}
public void retrive(final TableView tb)
{
final ArrayList<Product> products = new ArrayList<>();
AndroidNetworking.get(retrive_url)
.setPriority(Priority.HIGH)
.build()
.getAsJSONArray(new JSONArrayRequestListener() {
@Override
public void onResponse(JSONArray response) {
JSONObject jo;
Product p;
try {
for (int i=0;i<response.length();i++)
{
jo=response.getJSONObject(i);
int id=jo.getInt("id");
String name=jo.getString("name");
String quantity=jo.getString("quantity");
p = new Product();
p.setId(id);
p.setName(name);
p.setQuantity(quantity);
products.add(p);
}
adapter = new SimpleTableDataAdapter(c,new TableHelper(c).returnProductArray(products));
tb.setDataAdapter(adapter);
}catch (JSONException e)
{
Toast.makeText(c,"JSON Error"+e.getMessage(),Toast.LENGTH_LONG).show();
}
}
@Override
public void onError(ANError anError) {
anError.printStackTrace();
Toast.makeText(c,"unsuccessful : error is :"+anError.getMessage(),Toast.LENGTH_LONG).show();
}
});
}
}
output: its output of android studio emulator
OUTPUT: Its the output of logcat in android studio
I think the problem is in the content type of response. By default, the content type of any response from the PHP server is plain/text. Due to this, the android library considers it as plain text response instead of JSON response.
To fix your problem, you might need to set a response content type to application/json by adding following before print statement.
header('Content-Type: application/json');
Edit: As I can see in the output and your code, you are printing few extra strings with the output like Connection Successful and echo"in DBadapter"; (in DBadaper.php). Due to this, the output becomes invalid JSON. Just remove those echo statements and your code should work