java - 如何使用 URL 从数据库中打印表中的多条记录

标签 java android

我正在尝试从表中的 URL 打印数据,但只能打印一条记录。我需要所有可用的记录

我执行以下操作:

private class MyTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... params) {

        Intent myNewIntent=getIntent();


        URL url = null;

        try {
            url = new URL("http://192.168.59.1:8080/WebApplication2/cegepgim/mobile/message");

            HttpURLConnection client = null;

            client = (HttpURLConnection) url.openConnection();

            client.setRequestMethod("GET");

            int responseCode = client.getResponseCode();

            System.out.println("\n Sending 'GET' request to URL : " + url);

            System.out.println("Response Code : " + responseCode);

            InputStreamReader myInput = new InputStreamReader(client.getInputStream());

            BufferedReader in = new BufferedReader(myInput);
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            System.out.println("The response is " + response.toString());

            JSONObject mainObject = new JSONObject(response.toString());
            JSONArray jsonArray = mainObject.getJSONArray("message");

            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject message = jsonArray.getJSONObject(i);

                message_id =""+message.getInt ("message_id");
                sender_id =""+message.getInt ("sender_id");
                receiver_id =""+message.getInt("receiver_id");
                message_content = message.getString("message_content");
                message_date = message.getString("message_date");

            }


        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {


        tr1 = new TableRow(Main3Activity.this);
        mszid = new TextView(Main3Activity.this);
        mszid.setText(message_id);
        tr1.addView(mszid);


        sendid = new TextView(Main3Activity.this);
        sendid.setText(sender_id);
        tr1.addView(sendid);

        receid = new TextView(Main3Activity.this);
        receid.setText(receiver_id);
        tr1.addView(receid);

        mszcontent = new TextView(Main3Activity.this);
        mszcontent.setText(message_content);
        tr1.addView(mszcontent);

        mszdate = new TextView(Main3Activity.this);
        mszdate.setText(message_date);
        tr1.addView(mszdate);

        myTable1.addView(tr1);

        super.onPostExecute(result);
    }
}

程序运行并打印第一条记录,但我正在尝试打印 5 条记录。由于它是在循环外打印的,因此它不会打印所有记录。我不知道如何在带循环的表中打印。

最佳答案

您应该首先保存所有记录。这是你的问题,

 for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject message = jsonArray.getJSONObject(i);

                message_id =""+message.getInt ("message_id");
                sender_id =""+message.getInt ("sender_id");
                receiver_id =""+message.getInt("receiver_id");
                message_content = message.getString("message_content");
                message_date = message.getString("message_date");

            }

在此代码中,您实际上覆盖了先前从前一个 JSON 对象解析的值。最终,您打印的是最后一条记录。

您应该使用 ArrayList 并保存所有记录,然后使用 ArrayList 上的 for 循环将它们显示在表中。

关于java - 如何使用 URL 从数据库中打印表中的多条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56063335/

相关文章:

java - 当我尝试连接到服务器时出现“远程服务器超时”异常

java - 捕获导致异常的 UnknownHostException

java - android ListView 按钮控件

android - 如何获取sqlite的数据并将其放入FragmentList并单击item

Android Studio 3.0.1 显示模拟器 : Process finished with exit code 1

android - 无法在eclipse中更改包名称

java - Spring 数据JPA : How to fix NullPointerException in findById method of CrudRepository?

java - 后台线程不起作用,但使用简单的 'System.out' 就可以了

java - 您的内容必须有一个 id 属性为 'android.R.id.list' 错误的 ListView

java - 成功将元素解析到列表后,SAX 解析器 endDocument 中的空列表