Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send a string on Android with HttpPost without using nameValuePairs

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();

The problem is that I cant do that, because I have to connect to a resource that receive a String with XML format.

Any idea about how can I send only the String without using a List<nameValuePair>

like image 877
Fernando Avatar asked Apr 18 '13 10:04

Fernando


1 Answers

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();
like image 129
Praful Bhatnagar Avatar answered Sep 21 '22 19:09

Praful Bhatnagar