---
title: "Http Post请求"
author: "lmx227"
date: "2014-08-04T16:10:01Z"
tags: []
categories: ["移动开发"]
slug: "http-post"
draft: false
lastmod: "2014-08-04T16:10:01Z"
id: 2100108
url: "https://lmx227.iteye.com/blog/2100108"
---

```
	/**
	 * 创建相册请求
	 * 参数：
	 * 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();
	}
```