本文主要是使用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();
}
}