Android使用httpPost向服务器发送请求 Android使用httpPost向服务器发送请求的方法
q757989418 人气:0想了解Android使用httpPost向服务器发送请求的方法的相关内容吗,q757989418在本文为您仔细讲解Android使用httpPost向服务器发送请求的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,httpPost,服务器,发送请求,下面大家一起来学习吧。
本文实例讲述了Android使用httpPost向服务器发送请求的方法。分享给大家供大家参考,具体如下:
import java.util.List; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.ConnectTimeoutException; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.CoreConnectionPNames; import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; import android.util.Log; public class RequestByHttpPost { public static String TIME_OUT = "操作超时"; public static String doPost(List<NameValuePair> params,String url) throws Exception{ String result = null; // 新建HttpPost对象 HttpPost httpPost = new HttpPost(url); // 设置字符集 HttpEntity entity = new UrlEncodedFormEntity(params, HTTP.UTF_8); // 设置参数实体 httpPost.setEntity(entity); // 获取HttpClient对象 HttpClient httpClient = new DefaultHttpClient(); //连接超时 httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30000); //请求超时 httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 30000); try { // 获取HttpResponse实例 HttpResponse httpResp = httpClient.execute(httpPost); // 判断是够请求成功 if (httpResp.getStatusLine().getStatusCode() == 200) { // 获取返回的数据 result = EntityUtils.toString(httpResp.getEntity(), "UTF-8"); Log.i("HttpPost", "HttpPost方式请求成功,返回数据如下:"); Log.i("result", result); } else { Log.i("HttpPost", "HttpPost方式请求失败"); } } catch (ConnectTimeoutException e){ result = TIME_OUT; } return result; } }
可以直接用的完整类。
希望本文所述对大家Android程序设计有所帮助。
加载全部内容