android - 从 WebService 加载 ListView

标签 android web-services http tomcat servlets

您好,我正在尝试通过网络服务将数据库中的数据加载到我的 ListView 中。我曾尝试分两次进行此操作,但我对每个阶段都不是很有信心。

  1. 从我的 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();
    

我的问题是,这是从网络服务返回列表的正确方法吗?

  1. 第二阶段是获取列表到我的 ListView ,这里是我的代码

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

这似乎只在网络服务返回单行文本时有效。如何从 Http 请求中检索类似数组的内容?

谢谢!

最佳答案

WebService 只能返回 xml 或字符串类型,因此您可以将您的列表放入格式如下的字符串中:

"this is a string 1; this is a string 2; this is a string 3"

服务器端看起来像:

  StringBuilder myList = new StringBuilder();

 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.append( result1.getString(1) + ";");               
  }
} catch (Exception e1) {
  e1.printStackTrace();
 }

out.println(mylist.toString());
out.close();

然后从Android客户端,你可以通过String.split(";");
这是一个例子:

private String inputStreamToString(InputStream is)
{
String line = "";
StringBuilder total = new StringBuilder();
// Wrap a BufferedReader around the InputStream
BufferedReader rd = new BufferedReader(new InputStreamReader(is), 1024 * 4);
// Read response until the end
try
{

    while ((line = rd.readLine()) != null)
    {
        total.append(line);
    }
} catch (IOException e)
{
    Log.e(TAG, "error build string" + e.getMessage());
}
// Return full string
return total.toString();
 }

InputStream is = response.getEntity().getContent();
String strResponse = inputStreamToString(is);
String mylist[] = strResponse .split(";");

或者你可以使用 JSON 解析一个列表对象并从 WebService 传输到 Android 客户端

关于android - 从 WebService 加载 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10875535/

相关文章:

c# - 底层连接已关闭 : The connection was closed unexpectedly

ruby-on-rails - 如何消除 ActionController::UnknownHttpMethod 错误?

android - 调整由 BitmapFactory.decodeByteArray() 生成的图像

multithreading - 在 Mojolicious 中同时获取数据

android - Gallery 中的 WebView 停止滚动/触摸事件

web-services - 为什么有些 API 提供者需要 API key ?

spring - 从 spring Controller 中的 http 上下文读取变量

python - 在另一个函数中跟踪一个函数的进度

android - 如何添加通知计时器,例如 whatsapp 来电通知?

Android TTS 语言