Send string to Android using HttpPost without using nameValuePairs

I was looking for information on how I can send information using the 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 cannot do this, because I need to connect to a resource that receives the String format with XML.

Any idea on how I can only send a string without using List<nameValuePair>

+6
source share
3 answers

Have you tried using StringEntity ? Above code can be updated to use StringEntity . The 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(); 
+19
source

You can use JSON as the post parameter. Try contacting FlexJson

0
source
 // Sending HTTPs Requet to Server try { Log.v("GG", "Sending sever 1 - try"); // start - line is for sever connection/communication HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost( "10.0.0.1/abc.php"); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); nameValuePairs.add(new BasicNameValuePair("qrcode", contents)); httppost.setEntity(new UrlEncodedFormEntity( nameValuePairs)); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); // end - line is for sever connection/communication InputStream is = entity.getContent(); Toast.makeText(getApplicationContext(), "Send to server and inserted into mysql Successfully", Toast.LENGTH_LONG) .show(); // Execute HTTP Post Request response= httpclient.execute(httppost); entity = response.getEntity(); String getResult = EntityUtils.toString(entity); Log.e("response =", " " + getResult); } catch (Exception e) { Log.e("log_tag", "Error in http connection " + e.toString()); } 
-1
source

All Articles