データベースから受け取った日付を表示するのに問題があります。私がしていることを説明しましょう。私はデータベースがあります。そこにはrate1というテーブルがあります。このテーブルはvalueという単一の列から形成され、double値を含んでいます。私の意図は、この列のすべてのデータを取得し、平均してJavaコードで返します。私は、TextViewに平均を表示したいと思っています。私の問題は、TextViewに何も表示されないということです。誰かが私が間違っている場所を教えてもらえますか?もし私が間違いの解決策を教えてもらえますか?今私は、コードを追加します。 これはAverage.javaです:データベースから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);
}
}
これは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";
}
そして、これは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);
?>
正しいデータが表示(ログ)できるようになるとすぐに教えてください。応答は大丈夫ですか? JSONObject?エラーメッセージはありますか? – Jeff
** 1。** PHPは動作しましたか? ** 2。** URLConnectionは機能しましたか? ** 3。**構文解析が機能しましたか?例外はありますか? – AxelH
どのようにConfigから 'value'を回復しますか?SQLでは' average'という名前です。しかし、配列の名前が 'result'であるかどうかはわかりません。 'AVG()'は1行しか返さないので、配列の代わりに単純なJsonオブジェクトを返すことができます – AxelH