HttpURLConnection-POST-GSON

本文主要是使用httpURLConnection的post方法来与服务器进行数据交互。其中数据的封装方式为GSON。

直接上代码


<pre name="code" class="java">package dino.EasyPay.Wkzf;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

import org.json.JSONObject;

import android.util.Log;




/** 
 * 说明:这是封装的一个httpurlconnetcion的工具类。接收Map类型的输入参数,在将这个Map转换成JSON作为post的数据。 
 *  
 * @author syd 
 *  
 */  





public class MyHttpUtil {

	private final static String TAG = "sydlog";
	private static String unAESPostParams;
	private static String AESPostParams;
	private static URL postUrl;
	private static HttpURLConnection connection;
	private static DataOutputStream out;
	private static InputStream in;

	
	
	
	
	/**
	 * 开始用post数据交互,其中,数据的封装格式为json 
	 * @param url
	 * @param postDate
	 * @param listener
	 */
	
	
	
	
	
	public static void postJson(final String url,
			final Map<String, String> postDate,
			final HttpCallbackListener listener) {
		
		/**
		 * 网络操作都是耗时操作,开启子线程去请求网络
		 */

		new Thread(new Runnable() {

			@Override
			public void run() {

				String unAESReciiveDatea = null;
				try {

					/**
					 * 初始化Post参数
					 * 
					 * 
					 */

					postUrl = new URL(url);
					connection = (HttpURLConnection) postUrl.openConnection();
					connection.setConnectTimeout(3000);
					connection.setReadTimeout(3000);
					connection.setRequestMethod("POST");
					connection.setDoInput(true);
					connection.setDoOutput(true);
					connection.setUseCaches(false);

					/**
					 * 设置Post参数 ,转换成json格式字符串
					 */
					unAESPostParams = new JSONObject(postDate).toString();
					Log.e(TAG, "unAESPostParams" + unAESPostParams);

					/**
					 * Post AESPostParams
					 */
					AESPostParams = AESUtils.encode(unAESPostParams);

					// Log.e(TAG, "PostAESPostParams");

					/**
					 * 开始连接服务器
					 */
					connection.connect();
					// Log.e(TAG, "connect server");

					/**
					 * 向服务器发送数据 
					 */
					out = new DataOutputStream(connection.getOutputStream());
					out.write(AESPostParams.getBytes());
					out.flush();

					/**
					 * 从服务器接收数据 
					 */
					// Log.e(TAG, "start decode");
					int responseCode = connection.getResponseCode();
					if (responseCode == HttpURLConnection.HTTP_OK) {
						// Log.e(TAG, "receive success");
						in = connection.getInputStream();
						if (in != null) {
							/**
							 * 
							 */
							// byte[] receiveDate = new byte[1024];
							// int b = 0;
							// StringBuffer bufferReceive = new StringBuffer();
							// while ((b=(in.read(receiveDate))) != -1) {
							// bufferReceive.append(new
							// String(receiveDate,0,b));

							
							/**
							 * 从服务器接收到的数据流转成Json数据 
							 */
							BufferedReader reader = new BufferedReader(
									new InputStreamReader(in));
							StringBuffer bufferReceive = new StringBuffer();
							String line;
							while ((line = reader.readLine()) != null) {
								bufferReceive.append(line);
							}

							/**
							 * start decode
							 */
							unAESReciiveDatea = AESUtils.decode(bufferReceive
									.toString());
							// Log.e(TAG, "decode success");

							Log.e("sydlog", "unAESReciiveDatea="
									+ unAESReciiveDatea.toString());
							if (listener != null) {
								listener.onFinish(unAESReciiveDatea);
							}
						}
					} else {
						// Log.e("sydlog", "connection.getResponseCode()=" +
						if (listener != null) {
							listener.onError(new MyException(WkzfUrl.NO_200_OK));
						}
					}
				} catch (Exception e) {
					//
					if (listener != null) {
						listener.onError(e);
					}
				}

			}
		}).start();

	}

}



                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值