使用HttpPost在Android上发送字符串,而不使用nameValuePairs

Fer*_*ndo 6 string android http-post

我正在寻找有关如何在Android上使用HttpPost方法发送信息的信息,我总是看到:

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(posturl);

List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("Name","Var1"));
params.add(new BasicNameValuePair("Name2","Var2"));

httppost.setEntity(new UrlEncodedFormEntity(params));    
HttpResponse resp = httpclient.execute(httppost);
HttpEntity ent = resp.getEntity();
Run Code Online (Sandbox Code Playgroud)

问题是我无法做到这一点,因为我必须连接到接收XML格式的String的资源.

关于如何在不使用字符串的情况下仅发送字符串的任何想法 List<nameValuePair>

Pra*_*gar 19

你尝试过使用StringEntity吗?上面的代码可以更新使用StringEntity,以下是生成的代码:

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(posturl);


httppost.setEntity(new StringEntity("your string"));    
HttpResponse resp = httpclient.execute(httppost);
HttpEntity ent = resp.getEntity();
Run Code Online (Sandbox Code Playgroud)