您好我正在尝试通过网络服务从数据库中加载我的列表视图。我尝试过两次这样做,但我对每个阶段都不是很有信心。
从我的tomcat返回一个列表/数组这是代码
String mylist[] = {"", "", "", "", "", ""}; try { Class.forName(driver).newInstance(); con = DriverManager.getConnection(url + db, user, pass); String query1 = "select username from Users where online = 'yes'"; PreparedStatement preparedStmt1 = con.prepareStatement(query1); ResultSet result1 = preparedStmt1.executeQuery(); int i = 0; while (result1.next()) { mylist[i] = result1.getString(1); } } catch (Exception e1) { e1.printStackTrace(); } out.println(mylist); out.close();
我的问题是这是从网络服务返回列表的正确方法吗?
第二阶段是将列表获取到我的列表视图,这是我的代码
try { HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet(); URI webservice = new URI("http://192.168.0.3:8080/Users/users"); request.setURI(webservice); HttpResponse response = client.execute(request); BufferedReader rd = new BufferedReader( new InputStreamReader(response.getEntity() .getContent())); while ((rd.readLine()) != null) { ArrayList<String> list = rd.readLine(); } } catch (Exception e1) { e1.printStackTrace(); }
这似乎只在 web 服务返回单行文本时有效。如何从 Http 请求中检索类似数组的内容?
谢谢!