[英]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.