简体   繁体   English

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

[英]How to print more than one record in Table from database with URL

I am trying to print the data from a URL in Table but I'm only able to print the one record. 我正在尝试从表中的URL打印数据,但我只能打印一条记录。 I need all the records those are available 我需要所有可用的记录

I do the following: 我做以下事情:

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

The program runs and print the first record, but there are 5 records that I'm trying to print. 该程序运行并打印第一条记录,但我正在尝试打印5条记录。 As it's printing out of the loop so it's not printing all records. 因为它打印出循环所以它不打印所有记录。 I'm not sure how to print in Table with loop. 我不知道如何在循环中打印表格。

You should be saving all the records in the first place. 您应该首先保存所有记录。 Here's your problem, 这是你的问题,

 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");

            }

In this code, you are actually over-writing the previously parsed valued from the previous JSON object. 在此代码中,您实际上是在覆盖先前解析的值来自先前的JSON对象。 Eventually, what you are printing is the last record. 最终,您正在打印的是最后一条记录。

You should use an ArrayList and save all the records and then display them in your table using a for loop on the ArrayList. 您应该使用ArrayList并保存所有记录,然后使用ArrayList上的for循环将它们显示在表中。

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

相关问题 如果存在两个以上的一条记录,则从数据库中获取数据 - Getting data from database if two more than one record is present 无法通过Maven从Java向Neo4j数据库插入多个记录 - Unable to insert more than one record into Neo4j database from java through maven 如何从数据库查看jsp页面中的多个图像? - How to view more than one image in a jsp page from database? 如何在文本区域中显示数据库中的多行 - How to display more than one row from database in a textarea 如何在我的HashMap中添加多个数据库记录? - How can I add more than one database record to my HashMap? 如何通过外键将mySQL数据库中的一个表连接到多个表? - How do i connect one table in a mySQL database to more than one other table via foreign keys? 如何从返回多个记录的java调用RPGIV程序 - How to Call RPGIV program from java that returns more than one record 在Java中仅单击一次按钮后如何从多个文本字段打印 - How i can print from more than one textfield after clicking the button just once in java 如何在salesforce中一次删除多条记录? - How to delete more than one record at a time in salesforce? 如何在Avro架构中包含多个记录? - How to include more than one record in Avro schema?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM