android – JSON Exception value connection of type java.lang.String cannot be converted to JSONArray-ThrowExceptions

Exception or error:

PHP code :(DBadapter.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)) {
    			print(json_encode(array("CANT retrive data")));






require_once 'DBadapter.php';

$dbadapter=new DBadapter();


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


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;

public class MySQLClient {

    private static final String retrive_url="";

    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<>();
                .getAsJSONArray(new JSONArrayRequestListener() {
                    public void onResponse(JSONArray response) {
                        JSONObject jo;
                        Product p;
                        try {
                            for (int i=0;i<response.length();i++)
                                int id=jo.getInt("id");
                                String name=jo.getString("name");
                                String quantity=jo.getString("quantity");

                                p = new Product();

                            adapter = new SimpleTableDataAdapter(c,new TableHelper(c).returnProductArray(products));

                        }catch (JSONException e)
                            Toast.makeText(c,"JSON Error"+e.getMessage(),Toast.LENGTH_LONG).show();


                    public void onError(ANError anError) {
                        Toast.makeText(c,"unsuccessful : error is :"+anError.getMessage(),Toast.LENGTH_LONG).show();



output: its output of android studio emulator

its output of android studio emulator

OUTPUT: Its the output of logcat in android studio

Its the output of logcat in android studio

Its the output of index.php opened in webbrowser

How to solve:

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');

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

Leave a Reply

Your email address will not be published. Required fields are marked *