Http Post在Android中无效

时间:2012-11-22 13:53:32

标签: android http-post

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

提前致谢。

1 个答案:

答案 0 :(得分: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, send and receive XML via HTTP POST method