android网络编程http的get,post方式

来源:互联网 发布:淘宝代购能退货吗 编辑:程序博客网 时间:2024/05/06 09:42

1、activity开启线程访问http

new Thread(new Runnable(){@Overridepublic void run() {// TODO Auto-generated method stubtry{String url = HttpUtil.BASE_URL + "queryYEServlet";ye = HttpUtil.queryStringForPost(url,sfz);yeView.setText(ye);Log.i("ye", ye);}catch(Exception e){Log.i("Register",e.toString());}}}).start();
2、post访问方式

第一种:

public static String queryStringForPost(String url, String sfz) {// 第一步,创建HttpPost对象HttpPost httpPost = new HttpPost(url);String result = null;HttpResponse response= null;// 设置HTTP POST请求参数必须用NameValuePair对象List<NameValuePair> params = new ArrayList<NameValuePair>();params.add(new BasicNameValuePair("sfz", sfz));Log.i("params", params + "");try {// 设置httpPost参数httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));httpPost.addHeader("Content-Type", "application/xml;charset=utf-8");Log.i("httpPost", httpPost + "");// 第二步,使用execute方法发送HTTP POST请求,并返回HttpResponse对象//response = HttpUtil.getHttpResponse(httpPost);response = new DefaultHttpClient().execute(httpPost);Log.i("response", response.toString());Log.i("status", response.getStatusLine().getStatusCode() + "");// 判断是否请求成功if (response.getStatusLine().getStatusCode() == 200) {// 获得响应result = EntityUtils.toString(response.getEntity());return result;}} catch (ClientProtocolException e) {e.printStackTrace();result = "网络异常!!!";return result;} catch (IOException e) {e.printStackTrace();result = "IO网络异常!";return result;}return null;}

url为访问地址,不带参数

第二种:
public static Map<String, Object> getConnectionData(String data,String urlSource) throws Exception {Map<String, Object> map = new HashMap<String, Object>();String result = null;boolean network = false;URL url;HttpURLConnection conn = null;InputStream inStream = null;ByteArrayOutputStream out = null;byte[] entity = null;if (data != null)entity = data.getBytes();try {url = new URL(urlSource);conn = (HttpURLConnection) url.openConnection();conn.setConnectTimeout(3000);conn.setDoOutput(true);// 允许对外输出数据conn.setDoInput(true);conn.setRequestMethod("POST");conn.setUseCaches(false);// 设置HTTP请求头conn.setRequestProperty("Accept","image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");conn.setRequestProperty("Accept-Language", "zh-CN");conn.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");if (entity != null)conn.setRequestProperty("Content-Length",String.valueOf(entity.length));conn.setRequestProperty("Connection", "Keep-Alive");// 发送参数if (entity != null) {DataOutputStream outStream1 = new DataOutputStream(conn.getOutputStream());outStream1.write(entity);// 把参数发送出去outStream1.flush();outStream1.close();}// 请求成功if (conn.getResponseCode() == 200) {network = true;inStream = conn.getInputStream();out = new ByteArrayOutputStream();int len = -1;byte[] buffer = new byte[1024];while ((len = inStream.read(buffer)) != -1) {out.write(buffer, 0, len);}out.flush();result = out.toString();}} catch (Exception e) {Log.i("WebConnection-getConnectionData()", e.toString());} finally {// 关闭if (out != null)out.close();if (inStream != null)inStream.close();if (conn != null)conn.disconnect();}map.put("result", result);map.put("network", network);return map;}
data格式如“name=”+name+"&age="+age,urlSource为访问IP
3、get访问方式
public static String queryStringForGet(String url) {// 获得HttpGet对象HttpGet request = HttpUtil.getHttpGet(url);String result = null;try {// 获得响应对象HttpResponse response = HttpUtil.getHttpResponse(request);// 判断是否请求成功if (response.getStatusLine().getStatusCode() == 200) {// 获得响应result = EntityUtils.toString(response.getEntity());return result;}} catch (ClientProtocolException e) {e.printStackTrace();result = "网络异常!";return result;} catch (IOException e) {e.printStackTrace();result = "网络异常!";return result;}return null;}

0 0
原创粉丝点击