【问题标题】:Retrive data from database to java从数据库中检索数据到java
【发布时间】: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 上......我试图像你说的那样改变,但它还没有向我显示任何东西;你能给我写一个更清楚的解决方案吗?
  • 公平地说,有人为你做了这件事;)你很快就问了这个问题,而没有尝试自己调试任何东西。这是一种不好的做法,对您毫无帮助。

标签: java php android mysql


【解决方案1】:

我认为这里出现了一个小问题;

JSONObject jsonObject = new JSONObject(response);

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

假设与服务器建立连接成功,并返回$result数组作为响应。

在这种特殊情况下,json_encode 函数返回的 json 数组未使用名称 result 进行包装。相反,它们只是包装在一起的 JSON 对象。

我能想出的可能解决方案是删除我提到的上述行并替换为;

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(); 
}

【讨论】:

  • 它不起作用 Error:(59, 34) error: cannot find symbol method size()
  • 天哪..试试 length();
  • 好的,谢谢;它有效,但在 TextView {"average":"1.2565489"} 中向我显示这个...现在我怎样才能只获得数字?
  • 您想知道如何在文本视图中显示列值吗?
  • 因为我想要变量中的数字
猜你喜欢
  • 2011-08-16
  • 1970-01-01
  • 2013-10-31
  • 2021-07-02
  • 2023-04-04
  • 2019-11-19
相关资源
最近更新 更多