Android 网络视图 POST 请求
Android Webview POST request
我正在尝试使用 Android 上的 WebView 执行 post 请求。
在搜索了几天并尝试了几十种方法后,我无法让它工作。在 SWIFT 中只有几行代码,所以我认为还必须有一种简单的方法来对 android 上的 Web 视图执行 post 请求。
由于(2016 年)EncodingUtils 和 HTTPClient 已弃用,这是我目前的方法:
String url = "http://example.com/php.php";
String postData = null;
postData = "param1=" + URLEncoder.encode("1234567890", "UTF-8");
webcontent.postUrl(url,postData.getBytes());
//or
webcontent.postUrl(url, Base64.encode(postData.getBytes(), Base64.DEFAULT));
两者都只会导致空白屏幕。只有一个参数要发送,并且应该从服务器接收到一个包含 html 的字符串。
此外,服务器上的 php returns 一个带有彩色背景的 html 字符串,与任何输入无关,但即使这样也不会显示,所以也许整个请求永远不会到达服务器?
提前致谢!
在 Android 中,您 不要 使用 webView
访问 HTTP 响应的内容。为此,您需要使用 HttpClient!
看这个nice tutorial which explains the fundamentals! Also see this video如果觉得难!
希望对您有所帮助!
我正在尝试使用 Android 上的 WebView 执行 post 请求。 在搜索了几天并尝试了几十种方法后,我无法让它工作。在 SWIFT 中只有几行代码,所以我认为还必须有一种简单的方法来对 android 上的 Web 视图执行 post 请求。 由于(2016 年)EncodingUtils 和 HTTPClient 已弃用,这是我目前的方法:
String url = "http://example.com/php.php";
String postData = null;
postData = "param1=" + URLEncoder.encode("1234567890", "UTF-8");
webcontent.postUrl(url,postData.getBytes());
//or
webcontent.postUrl(url, Base64.encode(postData.getBytes(), Base64.DEFAULT));
两者都只会导致空白屏幕。只有一个参数要发送,并且应该从服务器接收到一个包含 html 的字符串。 此外,服务器上的 php returns 一个带有彩色背景的 html 字符串,与任何输入无关,但即使这样也不会显示,所以也许整个请求永远不会到达服务器? 提前致谢!
在 Android 中,您 不要 使用 webView
访问 HTTP 响应的内容。为此,您需要使用 HttpClient!
看这个nice tutorial which explains the fundamentals! Also see this video如果觉得难!
希望对您有所帮助!