Http Post请求

	/**
	 * 创建相册请求
	 * 参数:
	 * name:相册名
	 * description:相册描述
	 * tags:标签
	 * uid:uid
	 */
	private void createAlbum()
	{
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("uid", String.valueOf(1102130));
		params.put("name", "my_album2");
		params.put("description", "this is description2");
		params.put("tags", "this is tags2");
		FastHttp.ajax(ContactCons.URL_CREATE_ALBUM, params, new AjaxCallBack() {
			
			@Override
			public void callBack(ResponseEntity arg0) {
				switch (arg0.getStatus()) {
				case FastHttp.result_ok:
					Log.d("", "CREATE_ALBUM-->"+arg0.getContentAsString());
					break;

				default:
					break;
				}
			}
			
			@Override
			public boolean stop() {
				return false;
			}
		});
	}
	
	/**
	 * 创建相册请求
	 */
	private void createAlbum2()
	{
		new Thread(new Runnable()
		{
			@Override
			public void run()
			{
				// 发送请求
				HttpURLConnection conn = null;// http连接
				OutputStream output = null;// 输出流
				InputStream is = null;// 输入流
				StringBuilder buf = new StringBuilder();
				BufferedReader br = null;
				String result = "";
				
				try
				{
					buf.append("name=" + URLEncoder.encode("my_album") + "&");
					buf.append("description=" + URLEncoder.encode("this is description") + "&");
					buf.append("tags=" + URLEncoder.encode("this is tags") + "&");
					buf.append("uid=" + URLEncoder.encode("1102130"));
					byte[] data = buf.toString().getBytes();
					URL url = new URL(ContactCons.URL_CREATE_ALBUM);
					conn = (HttpURLConnection) url.openConnection();
					conn.setRequestMethod("POST");
					conn.setDoOutput(true);
					conn.setConnectTimeout(5000);
					output = conn.getOutputStream();
					output.write(data);
					output.flush();
					output.close();

					// 接收响应
					if (conn.getResponseCode() == HttpURLConnection.HTTP_OK)
					{
						is = conn.getInputStream();
						br = new BufferedReader(new InputStreamReader(is));
						String line;
						while ((line = br.readLine()) != null)
						{
							result += line;
						}
						Log.d("", result);
						is.close();
					}
				}
				catch (UnsupportedEncodingException e)
				{
					e.printStackTrace();
				}
				catch (IOException e)
				{
					e.printStackTrace();
				}
				finally
				{
					if(conn != null)
					conn.disconnect();
				}
			}
		}).start();
	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值