php - Retrive data from database to java


I have a problem to view a date that I receive from my database. Let me explain what I'm doing. I have a database where there is a table called rate1 which is formed from a single column called value which it contains double numbers. My intent is to take all the data in this column, do the average and return it in my java code. I wanto to display the average in a TextView. My problem is that I do not see anything on the TextView. Can anyone tell me where I'm wrong, and if can he give me solutions to my mistakes? Now I add the code: This is the

import android.os.Bundle;

import android.widget.TextView;
import android.widget.Toast;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Average extends AppCompatActivity {

private TextView textViewResult;

private ProgressDialog loading;

protected void onCreate(Bundle savedInstanceState) {

    textViewResult = (TextView) findViewById(;

    loading =,"Please wait...","Fetching...",false,false);

    String url = Config.DATA_URL.toString().trim();

    StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
        public void onResponse(String response) {
            new Response.ErrorListener() {
                public void onErrorResponse(VolleyError error) {

    RequestQueue requestQueue = Volley.newRequestQueue(this);

private void showJSON(String response){
    String name="";
    try {
        JSONObject jsonObject = new JSONObject(response);
        JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY);
        JSONObject collegeData = result.getJSONObject(0);
        name = collegeData.getString(Config.KEY_NAME);
    } catch (JSONException e) {


This is the

public class Config {
public static final String DATA_URL = "";
public static final String KEY_NAME = "value";
public static final String JSON_ARRAY = "result";

And this is Average.php:

  $connect = mysqli_connect("********", "********", "********", "*******");
$response = mysqli_query($connect, 'SELECT AVG(value) AS average FROM rate1');
$result = array();
if ($response) {

/* fetch associative array */
while ($row = mysqli_fetch_assoc($response)) {
    $result[] = $row;

/* free result set */
echo json_encode($result);



I think that the a little problem comes up here;

JSONObject jsonObject = new JSONObject(response);


JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY);

Let's suppose that the connection is established successfully with the server and$result array is returned as response.

In this particular case, the json array returned by thejson_encode function is not wrapped with the nameresult. Instead they are just wrapped JSON objects connected together.

The possible solution I can come up with is to delete the above lines I mentioned and replace them with;

try { 
JSONArray result = new JSONArray(response);
for(int i=0; i<result.length(); i++){
   JSONObject obj = result.getJSONObject(i);
  // Proceed with your logic with each object accordingly.
} catch (JSONException e) { e.printStackTrace(); 

People are also looking for solutions to the problem: php - how to create middleware in octobercms


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.