0

我正在使用一个将 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;
}

提前致谢。

4

1 回答 1

0
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.192.131/");

try {
    StringEntity se = new StringEntity( "<aaaLogin inName=\"admin\" inPassword=\"admin123\"/>", HTTP.UTF_8);
    se.setContentType("text/xml");
    httppost.setEntity(se);

    HttpResponse httpresponse = httpclient.execute(httppost);
    HttpEntity resEntity = httpresponse.getEntity();
    tvData.setText(EntityUtils.toString(resEntity));

} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Android,通过 HTTP POST 方法发送和接收 XML

于 2013-06-18T17:10:00.917 回答