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

1、activity开启线程访问http

new Thread(new Runnable(){
			@Override
			public void run() {
				// TODO Auto-generated method stub
				try{
					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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值