我正在使用一个将 XML 数据作为 Web 服务的 android 应用程序。我的任务是通过使用 HTTP Post 发布值并检索数据并在列表视图中显示来添加数据。在发布时,我需要将其加密为 UTF-8 格式。
下面我给出了我的代码。
private String postSyncXML() {
String url = strMainUrl + strSubUrl;
HttpClient httpclient = new DefaultHttpClient();
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs
.add(new BasicNameValuePair("uid", strUniqueId));
nameValuePairs.add(new BasicNameValuePair("first_name", strFirstName));
nameValuePairs.add(new BasicNameValuePair("last_name", strLastName));
nameValuePairs.add(new BasicNameValuePair("email", strEmail));
nameValuePairs.add(new BasicNameValuePair("phone", strPhone));
nameValuePairs.add(new BasicNameValuePair("notes", strNotes));
nameValuePairs.add(new BasicNameValuePair("is_sms", strIsSms));
nameValuePairs.add(new BasicNameValuePair("is_email", strIsEmail));
UrlEncodedFormEntity form;
try {
form = new UrlEncodedFormEntity(nameValuePairs, "UTF-8");
HttpPost httppost = new HttpPost(url);
Log.d("", "Add Guest URL "+url);
httppost.setEntity(form);
HttpResponse response = (HttpResponse) httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
String resp = EntityUtils.toString(resEntity);
Log.i("Method call", "postSyncXML srv response:" + resp);
return resp;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
提前致谢。