Mega Code Archive

 
Categories / Android / Network
 

Send message with HttpPost

//package com.javaseed; import java.io.IOException; import java.util.List; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.ResponseHandler; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.BasicResponseHandler; import org.apache.http.impl.client.DefaultHttpClient; public class HttpClientUtil {   protected String sendMsg(String url) throws ClientProtocolException,       IOException {     return sendMsg(url, null);   }   protected String sendMsg(String url, List<NameValuePair> params)       throws ClientProtocolException, IOException {     HttpClient httpclient = new DefaultHttpClient();     HttpPost httppost = new HttpPost(url);     System.out.println("executing request " + httppost.getURI());     if (params != null) {       UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params,           "UTF-8");       httppost.setEntity(entity);     }     ResponseHandler<String> responseHandler = new BasicResponseHandler();     String responseBody = httpclient.execute(httppost, responseHandler);     httpclient.getConnectionManager().shutdown();     return responseBody;   } }