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

时间:2019-05-09 15:55:47

标签: java android

我正在尝试从Table中的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条记录。由于它是循环打印的,因此不会打印所有记录。我不确定如何在带有循环的表格中打印。

1 个答案:

答案 0 :(得分:0)

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

 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循环在表中显示它们。