Q:

how to get from MySQL data to android with json

I was tried to get data from MySQL PHP to Android with JSON Object but not work with me. I was searching about my problem but the examples I found didn't help me.
I have an array list of strings, then I set the strings  MySQL DB.
After that, I want to get the cities strings from the DB with JSON, but I was unsuccessful.
My questions are:

How can I make sure that if I have city, it won't appear again?
How can I set the cities in an array list in Android?

My PHP code:
<?php
include 'connection/connection.php';
$noResult = "no results";

    // to set the names at the list view
$sql = "SELECT workrCity FROM workersTable ";

$result = $connect->query($sql);

if ($result->num_rows > 0){ 
while($row[] = $result->fetch_assoc()) {

 $json = json_encode($row,JSON_UNESCAPED_UNICODE);

}
} else {
echo $noResult;
}
echo $json;
$connect->close();
?>

the array list function in my Fragment working good :
    private ArrayList<City> initCities() {
    Log.d(TAG, "ArrayList_CitiesFragment_initCities");
    String[] cityName = {"","","",""}; // the cities names
    ArrayList<City> theCities = new ArrayList<>();
    for (String aCityName : cityName) {
        City city = new City(aCityName, false);
        theCities.add(city);
    }
    return theCities;
}

Now I want to get the cities names from MySQL in a JSON-like output:
       handler = new Handler(Looper.getMainLooper());
    Thread runner = new Thread(new Runnable() {
        @Override
        public void run() {
            Log.d(TAG, " runner");
            GetCitiesJson getCitiesJson = new GetCitiesJson();
            try{

                String[] res = getCitiesJson.getCitiesDataFromDB();
                JSONObject jsonObject = new JSONObject(String.valueOf(res));
                JSONObject workrCity = jsonObject.getJSONObject("workrCity");
                Activity activity = getActivity();
                Toast.makeText(activity,"its :" + workrCity, Toast.LENGTH_SHORT).show();

            } catch (IOException | JSONException e) {
                e.printStackTrace();
            }
        }
    });
    runner.start();

I know that my code is not correct, but I don't know what's missing...

A:

Hi: Im using Volley library to get a Json file from my server and works fine and fast.
In app/build.gradle under dependencies:
compile 'com.android.volley:volley:1.0.0'
Then, in the activity you want to get the json data:
String from = "http://www.yourserver.com/file.json";
StringRequest request = new StringRequest(from, new Response.Listener<String>() {
            @Override
            public void onResponse(String string) {
                try {
                    parseJson(URLDecoder.decode(URLEncoder.encode(string, "iso8859-1"),"UTF-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                // Error 
               Log.d("ERROR:", String.valueOf(volleyError));
            }
        });

        Volley.newRequestQueue(this).add(request);

Then you can parse the Json:
public static void parseJson(String jsonString) {

    try {
        JSONObject object = new JSONObject(jsonString);
        getMessage(this, object);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

And finally you can get the strings inside your Json:
public static void getMessage(JSONObject object){
 if(object.length() != 0) { 
try {

            message = String.valueOf(object.get("message"));

        } catch (JSONException e) {
            e.printStackTrace();
        }
} }

