Hello I’m trying to load my listview with data from a database via a webservice. I have tried to do this in two fold and I am not very confident in each stage.
-
return a list/array from my tomcat here is the code
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();
My question here would be is this the right way to return a list from a webservice?
-
The second stage is to get the list to my list view, here is my code
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(); }
This seems to only work when the webservice returns single lines of text. How do I retrieve something like an array from a Http request?
Thanks!
A WebService can only return xml or string type, so you can put your list into a string with format like this:
The server side look like:
Then from Android client side, you can get it by String.split(“;”);
Here is an example:
Or you can use JSON to parse an list object and transfer from WebService to Android client