我正在尝试将我的表单数据从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".
请指导我如何解决这个问题。
第二,HTTPClient等自API 22以来已被弃用,如果有人知道发布方法的最新教程(URLConnection),请与我分享,我将负债累累。
答案 0 :(得分:0)
来自Android sending post data to webservice
使用调用sendPostRequest(String username,String pass)方法 参数。您可以从UI线程(请求)中调用此方法 将从另一个线程发送(嵌入了AsyncTask)。
private void sendPostRequest(String givenUsername, String givenPassword) { class SendPostReqAsyncTask extends AsyncTask<String, Void, String> { @Override protected String doInBackground(String... params) { String paramUsername = params[0]; String paramPassword = params[1]; System.out.println("*** doInBackground ** paramUsername " + paramUsername + " paramPassword :" + paramPassword); HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost( "http://lib-dm.process9.com/libertydm/ValidateUserHandler.ashx"); //> replace with your url httpPost.addHeader("Content-type", "application/x-www-form-urlencoded"); BasicNameValuePair usernameBasicNameValuePair = new BasicNameValuePair( "UserId", paramUsername); // Make your own key value pair BasicNameValuePair passwordBasicNameValuePAir = new BasicNameValuePair( "Password", paramPassword);// make your own key value pair // You can add more parameters like above List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>(); nameValuePairList.add(usernameBasicNameValuePair); nameValuePairList.add(passwordBasicNameValuePair); try { UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity( nameValuePairList); httpPost.setEntity(urlEncodedFormEntity); try { HttpResponse httpResponse = httpClient .execute(httpPost); InputStream inputStream = httpResponse.getEntity() .getContent(); InputStreamReader inputStreamReader = new InputStreamReader( inputStream); BufferedReader bufferedReader = new BufferedReader( inputStreamReader); StringBuilder stringBuilder = new StringBuilder(); String bufferedStrChunk = null; while ((bufferedStrChunk = bufferedReader.readLine()) != null) { stringBuilder.append(bufferedStrChunk); } return stringBuilder.toString(); } catch (ClientProtocolException cpe) { System.out .println("First Exception coz of HttpResponese :" + cpe); cpe.printStackTrace(); } catch (IOException ioe) { System.out .println("Second Exception coz of HttpResponse :" + ioe); ioe.printStackTrace(); } } catch (UnsupportedEncodingException uee) { System.out .println("An Exception given because of UrlEncodedFormEntity argument :" + uee); uee.printStackTrace(); } return null; } @Override protected void onPostExecute(String result) { super.onPostExecute(result); } } SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask(); sendPostReqAsyncTask.execute(givenUsername, givenPassword); }