dpspn60064
dpspn60064
2014-05-05 04:18

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

已采纳

I currently have an app that shows a listview of data that it gets from the internet.

Similar to the tutorial Here I am using a HTML file, and a webview right now, in order to add new data to the online XML. Like this:

<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">

Now, anytime I fill in the above field "title" gets updated online by my .php script. That works flawlessly, with multiple fields.

I just want to know how I can get rid of the tacky webview. I created a custom form using editText, layouts, and buttons. But how can I use the new form to update the online XML. What is the best way to grab the data out of the editText and pass it to the <form action="http:site.com/test/update.php" method="GET"> file?

Thanks

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dongsonghen9931 dongsonghen9931 7年前

    You can use Android's built-in Http client to send a GET request to your server.

    1. Retrieve data from EditText and process it with URLEncoder it to prepare GET request:

      String getUrl = "http://example.com/test/update.php?data=" + URLEncoder.encode(editText.getText().toString(), "UTF-8");
      
    2. Make an Http GET request using HttpURLConnection Android Http client:

      URL url = new URL(getUrl);
      HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
      
    3. Read server response.

      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. Close connection:

      urlConnection.disconnect();
      
    点赞 评论 复制链接分享

相关推荐