Lorenzo Lorenzo - 15 days ago 3
Java Question

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 Average.java:

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

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

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

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

public class Average extends AppCompatActivity {

private TextView textViewResult;

private ProgressDialog loading;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_average);

textViewResult = (TextView) findViewById(R.id.textViewResult);

loading = ProgressDialog.show(this,"Please wait...","Fetching...",false,false);

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

StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
loading.dismiss();
showJSON(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Average.this,error.getMessage().toString(),Toast.LENGTH_LONG).show();
}
});

RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}

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) {
e.printStackTrace();
}
textViewResult.setText("Name:\t"+name);
}

}


This is the Config.java:

public class Config {
public static final String DATA_URL = "https://lbstudios.000webhostapp.com/Average.php";
public static final String KEY_NAME = "value";
public static final String JSON_ARRAY = "result";
}


And this is Average.php:

<?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 */
mysqli_free_result($result);
}
echo json_encode($result);
?>

Answer

I think that the a little problem comes up here;

JSONObject jsonObject = new JSONObject(response);

And

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 the json_encode function is not wrapped with the name result. 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(); 
}