如何使用Android EditText Data填充在线XML

时间:2014-05-05 04:18:07

标签: php android xml listview

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

与教程Here类似 我现在正在使用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">

现在,我随时填写上述字段&#34; title&#34;通过我的.php脚本在线更新。这是完美无缺的,有多个领域。

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

由于

1 个答案:

答案 0 :(得分:1)

您可以使用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 Android Http客户端发出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();