我正在开发一个 android 项目,在该项目中我需要使用 post 方法将两个 xml 作为参数发送到服务器(即我想以表单形式发送)。我尝试使用以下代码发送数据,但它不起作用。远程数据库中没有数据。
private void postFormData(List<DataItem> ti,String ex,String getExpensesXml)
{
//Create a new Http Client
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
try
{
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("sync","true"));
nameValuePairs.add(new BasicNameValuePair("tt",ti));
nameValuePairs.add(new BasicNameValuePair("te",ex));
UrlEncodedFormEntity form;
form = new UrlEncodedFormEntity(nameValuePairs,"UTF-8");
httppost.setEntity(form);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
String line = EntityUtils.toString(entity);
System.out.println(line);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
我找不到问题所在。如果有人设法找到问题并建议我解决方案,那就太好了。
我还有两个问题?我在尝试正确的代码吗?有没有其他方法可以通过 Form 将 xml 数据发送到服务器?
提前致谢