发送的Android字符串与HttpPost不使用namevaluepairs中
问题描述:
我一直在寻找,我怎么可以使用HttpPost Android上的方法来发送信息的信息,我总是看到以下内容:
I was looking information about how I can send information using HttpPost method on android, and I always see this:
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();
问题是,我不能这样做,因为我有连接到接收字符串的XML格式的资源。
The problem is that I cant do that, because I have to connect to a resource that receive a String with XML format.
如何我只能发送字符串,而无需使用任何想法列表与LT;&的NameValuePair GT;
Any idea about how can I send only the String without using a List<nameValuePair>
答
您是否尝试过使用StringEntity?上述code可以更新为使用 StringEntity
,以下是得到的code:
Have you tried using StringEntity? Above code can be updated to use StringEntity
, Following is the resulting code:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(posturl);
httppost.setEntity(new StringEntity("your string"));
HttpResponse resp = httpclient.execute(httppost);
HttpEntity ent = resp.getEntity();