如何使用Android EditText数据来填充在线XML


How to use Android EditText Data to populate online XML

我目前有一个应用程序,显示数据的列表视图,它从互联网获得。

类似于这里的教程我现在正在使用一个HTML文件和一个webview,以便向在线XML添加新数据。像这样:

<div id="stylized" class="myform">
<form action="http:site.com/test/update.php" method="GET">
    <label>Name
        <span class="small">Name of Event</span>
    </label>
<input type="text" name="title">

现在,只要我填写上面的字段"title"就会被我的。php脚本在线更新。它在多个字段中完美地工作。

我只是想知道如何摆脱俗气的webview。我使用editText、布局和按钮创建了一个自定义表单。但是如何使用新表单来更新在线XML呢?从editText中获取数据并将其传递给<form action="http:site.com/test/update.php" method="GET">文件的最佳方法是什么?

谢谢

你可以使用Android内置的Http客户端发送GET请求到你的服务器。

  1. 从EditText检索数据并使用URLEncoder处理它以准备GET请求:

    String getUrl = "http://example.com/test/update.php?data=" + URLEncoder.encode(editText.getText().toString(), "UTF-8");
    
  2. 使用HttpURLConnection发送Http GET请求:

    URL url = new URL(getUrl);
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
    
  3. 读取服务器响应。

    InputStream is = urlConnection.getInputStream();
    StringBuilder response = new StringBuilder();
    byte[] buffer = new byte[1024];
    int len = -1;
    while ((len = is.read(buffer)) != -1)
        s.append(new String(buffer, "UTF-8"));
    is.close();
    
  4. 紧密联系:

    urlConnection.disconnect();