【发布时间】:2017-04-07 11:06:40
【问题描述】:
我在查看从数据库中收到的日期时遇到问题。让我解释一下我在做什么。我有一个数据库,其中有一个名为 rate1 的表,它由一个名为 value 的列组成,其中包含双数。我的意图是获取此列中的所有数据,取平均值并将其返回到我的 java 代码中。我想在 TextView 中显示平均值。我的问题是我在 TextView 上看不到任何内容。谁能告诉我我哪里错了,如果他能给我解决我的错误的方法吗?现在我添加代码: 这是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);
}
}
这是 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);
?>
【问题讨论】:
-
你能告诉你什么时候可以看到(记录)正确的数据吗?反应正常吗? JSON对象?任何错误消息?
-
1. php 有效吗? 2. URLConnection 是否有效? 3. 解析是否有效。有什么例外吗?
-
如何...你从 Config 中恢复
value,但它在 SQL 中被命名为average。但我不确定该数组是否会命名为result。你可以返回一个简单的 Json 对象而不是一个数组,因为AVG()只会返回一行 -
所以我不确定 php 文件是否有效……我想是的……它没有返回任何错误……基本上我看到它连接了,打开了活动,什么也没有发生在 TextView 上......我试图像你说的那样改变,但它还没有向我显示任何东西;你能给我写一个更清楚的解决方案吗?
-
公平地说,有人为你做了这件事;)你很快就问了这个问题,而没有尝试自己调试任何东西。这是一种不好的做法,对您毫无帮助。