简体   繁体   English

如何将数据从 MySQL 数据库插入到表中显示到 Android?

[英]How to Display Data from MySQL Database to Android by inserting it into Table?

I'm Creating an Android App that shows tables which the data's came from MySQL Database.我正在创建一个显示数据来自 MySQL 数据库的表的 Android 应用程序。 I have my Codes Running without Errors.. BUT the data from DB is not displaying on the Table.我的代码运行没有错误..但是来自 DB 的数据没有显示在表上。 By the way i'm using Eclipse as my compiler.顺便说一下,我使用 Eclipse 作为我的编译器。

This is my Database :这是我的数据库:

在此处输入图片说明

This is My PHP codes with Json :这是我使用 Json 的 PHP 代码:

<?php 

$con=mysqli_connect("localhost","root","");
mysqli_select_db($con,"qstatslite")or die("error");


$q = mysqli_query($con,"SELECT * FROM queue_stats ORDER BY queue_stats_id DESC LIMIT 20");
while($row=mysqli_fetch_assoc($q))
$json_output[]=$row;

print(json_encode($json_output));



?>

And This is My Java Codes for DB :这是我的 DB 的 Java 代码:

import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

public class GetDataFromDB {

    public String getDataFromDB() {
        try {

            HttpPost httppost;
            HttpClient httpclient;
            httpclient = new DefaultHttpClient();
            httppost = new HttpPost(
                    "http:///192.168.1.87/Projects/Callchart/selectall.php"); 
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            final String response = httpclient.execute(httppost,
                    responseHandler);

            return response.trim();

        } catch (Exception e) {
            System.out.println("ERROR : " + e.getMessage());
            return "error";
        }
    }
}

And This is my MainActivity :这是我的 MainActivity :

import java.util.ArrayList;
import java.util.Iterator;

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

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TableRow.LayoutParams;
import android.widget.TextView;

public class MainActivity extends Activity {

    String data = "";
    TableLayout tl;
    TableRow tr;
    TextView label;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tl = (TableLayout) findViewById(R.id.maintable);

        final GetDataFromDB getdb = new GetDataFromDB();
        new Thread(new Runnable() {
            public void run() {
                data = getdb.getDataFromDB();
                System.out.println(data);

                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        ArrayList<Users> queue_stats = parseJSON(data);
                        addData(queue_stats);                       
                    }
                });

            }
        }).start();
    }

    public ArrayList<Users> parseJSON(String result) {
        ArrayList<Users> users = new ArrayList<Users>();
        try {
            JSONArray jArray = new JSONArray(result);
            for (int i = 0; i < jArray.length(); i++) {
                JSONObject json_data = jArray.getJSONObject(i);
                Users user = new Users();
                user.setqueue_stats_id(json_data.getInt("queue_stats_id"));
                user.setdatetime(json_data.getInt("datetime"));
                user.setqname(json_data.getInt("qname"));
                user.setqagent(json_data.getInt("qagent"));
                user.setqevent(json_data.getInt("qevent"));
                user.setinfo1(json_data.getString("info1"));
                user.setinfo2(json_data.getString("info2"));
                user.setinfo3(json_data.getString("info3"));
                users.add(user);
            }
        } catch (JSONException e) {
            Log.e("log_tag", "Error parsing data " + e.toString());  
        }
        return users;
    }


    void addHeader(){
        /** Create a TableRow dynamically **/
        tr = new TableRow(this);

        /** Creating a TextView to add to the row **/
        label = new TextView(this);
        label.setText("DateTime");
        label.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        label.setPadding(5, 5, 5, 5);
        label.setBackgroundColor(Color.GRAY);
        LinearLayout Ll = new LinearLayout(this);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT);
        params.setMargins(5, 5, 5, 5);
        //Ll.setPadding(10, 5, 5, 5);
        Ll.addView(label,params);
        tr.addView((View)Ll); // Adding textView to tablerow.

        /** Creating Qty Button **/
        TextView queue= new TextView(this);
        queue.setText("Queue");
        queue.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        queue.setPadding(5, 5, 5, 5);
        queue.setBackgroundColor(Color.GRAY);
        Ll = new LinearLayout(this);
        params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 5, 5, 5);
        //Ll.setPadding(10, 5, 5, 5);
        Ll.addView(queue,params);
        tr.addView((View)Ll); // Adding textview to tablerow.

        /** Creating Qty Button **/
        TextView agent = new TextView(this);
        agent.setText("Agent");
        agent.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        agent.setPadding(5, 5, 5, 5);
        agent.setBackgroundColor(Color.GRAY);
        Ll = new LinearLayout(this);
        params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 5, 5, 5);
        //Ll.setPadding(10, 5, 5, 5);
        Ll.addView(agent,params);
        tr.addView((View)Ll);

        TextView event = new TextView(this);
        event.setText("Event");
        event.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        event.setPadding(5, 5, 5, 5);
        event.setBackgroundColor(Color.GRAY);
        Ll = new LinearLayout(this);
        params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 5, 5, 5);
        //Ll.setPadding(10, 5, 5, 5);
        Ll.addView(event,params);
        tr.addView((View)Ll);

        TextView info1 = new TextView(this);
        info1.setText("Info1");
        info1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        info1.setPadding(5, 5, 5, 5);
        info1.setBackgroundColor(Color.GRAY);
        Ll = new LinearLayout(this);
        params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 5, 5, 5);
        //Ll.setPadding(10, 5, 5, 5);
        Ll.addView(info1,params);
        tr.addView((View)Ll);

        TextView info2 = new TextView(this);
        info2.setText("Info1");
        info2.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        info2.setPadding(5, 5, 5, 5);
        info2.setBackgroundColor(Color.GRAY);
        Ll = new LinearLayout(this);
        params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 5, 5, 5);
        //Ll.setPadding(10, 5, 5, 5);
        Ll.addView(info2,params);
        tr.addView((View)Ll);

        TextView info3 = new TextView(this);
        info3.setText("Info1");
        info3.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        info3.setPadding(5, 5, 5, 5);
        info3.setBackgroundColor(Color.GRAY);
        Ll = new LinearLayout(this);
        params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 5, 5, 5);
        //Ll.setPadding(10, 5, 5, 5);
        Ll.addView(info3,params);
        tr.addView((View)Ll);

         // Add the TableRow to the TableLayout
        tl.addView(tr, new TableLayout.LayoutParams(
                LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT));
    }

    @SuppressWarnings({ "rawtypes" })
    public void addData(ArrayList<Users> users) {

        addHeader();

        for (Iterator i = users.iterator(); i.hasNext();) {

            Users p = (Users) i.next();

            /** Create a TableRow dynamically **/
            tr = new TableRow(this);

            /** Creating a TextView to add to the row **/
            label = new TextView(this);
            label.setText(p.getdatetime());
            label.setId(p.getqueue_stats_id());
            label.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));
            label.setPadding(5, 5, 5, 5);
            label.setBackgroundColor(Color.GRAY);
            LinearLayout Ll = new LinearLayout(this);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                    LayoutParams.WRAP_CONTENT);
            params.setMargins(5, 2, 2, 2);
            //Ll.setPadding(10, 5, 5, 5);
            Ll.addView(label,params);
            tr.addView((View)Ll); // Adding textView to tablerow.

            /** Creating Qty Button **/
            TextView place = new TextView(this);
            place.setText(p.getqname());
            place.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));
            place.setPadding(5, 5, 5, 5);
            place.setBackgroundColor(Color.GRAY);
            Ll = new LinearLayout(this);
            params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                    LayoutParams.WRAP_CONTENT);
            params.setMargins(0, 2, 2, 2);
            //Ll.setPadding(10, 5, 5, 5);
            Ll.addView(place,params);
            tr.addView((View)Ll); // Adding textview to tablerow.

             // Add the TableRow to the TableLayout
            tl.addView(tr, new TableLayout.LayoutParams(
                    LayoutParams.MATCH_PARENT,
                    LayoutParams.WRAP_CONTENT));
        }
    }
}

Can someone help me solve this one?有人可以帮我解决这个问题吗?

This is the output so far.. No data display:这是到目前为止的输出..没有数据显示:

在此处输入图片说明

The problem is you have to encode the data you are getting from database into json with key value pairs.问题是您必须使用键值对将从数据库中获取的数据编码为 json。 But you are directly sending the rows as result to Android Application.但是您直接将行作为结果发送到 Android 应用程序。

Parse individual rows into arrays and encode the array into json.将单个行解析为数组并将数组编码为 json。

$return_arr = array();
while ($row = mysql_fetch_array($q)) 
{
    $row_array['queue_stats_id'] = $row['queue_stats_id'];
    $row_array['qname'] = $row['qname'];
    .
    .
    .
    array_push($return_arr,$row_array);
}
echo json_encode($return_arr);

Now you will get a json object consisting of arrays which you can parse in your java code in your Android Application.现在您将获得一个由数组组成的 json 对象,您可以在 Android 应用程序的 Java 代码中解析这些数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM