【发布时间】:2015-05-26 12:45:45
【问题描述】:
我正在尝试将我的表单数据从 android 发布到网站。我到处搜索,这是我通常找到的代码。
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://localhost/adminp/script.php");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("stringdata", "Hi"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
但是有两件事会造成问题。首先, UrlEncodedFormEntity 和执行都显示错误:
"Error:(94, 29) error: unreported exception UnsupportedEncodingException; must be caught or declared to be thrown" 和
"Error:(97, 52) error: unreported exception IOException; must be caught or declared to be thrown".
请指导我如何解决这个问题。
其次,自 API 22 以来,HTTPClient 等已被弃用,如果有人知道最新的发布方法教程(URLConnection),请与我分享,我会负债累累。
【问题讨论】:
标签: android forms post httpclient urlconnection