android post请求

	/**
	 * 发送请求
         *注:调用该方法时需要在子线程中调用!
	 */
	@SuppressWarnings("null")
	public void postService(String url,List<NameValuePair> list){
		//创建httppost对象
		HttpPost httpPost = new HttpPost(url);
		//创建BasicNameValuePair集合,用于存放参数       注:键值对模式
		HttpResponse httpResponse = null;
		try {
			//设置请求参数和编码
			httpPost.setEntity(new UrlEncodedFormEntity(list, HTTP.UTF_8));
			//发送请求
			httpResponse = new DefaultHttpClient().execute(httpPost);
			//获取请求返回状态i
			int i = httpResponse.getStatusLine().getStatusCode();
			//200请求成功
			if(i == 200){
				//获取返回请求结果
				String str = EntityUtils.toString(httpResponse.getEntity());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}
//新建子线程
	thread = new Thread(new Runnable() {
					
					@Override
					public void run() {
						
					}
				});
				thread.start();

猜你喜欢

转载自a754782339.iteye.com/blog/2267173