package com.connection;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import org.json.JSONObject;
import android.content.Context;
public class Connection {
public static String encoding="UTF-8";
public static String
baseUrl="http://192.168.1.101:8080/";
public static String postData(Context context,Map
params,String requestUrl) throws Exception{
String result=null;
String data="";
StringBuffer sb = new StringBuffer();
//组装数据,形如:aaa=admin&bbb=123
// if(params!=null){
// for (Map.Entry entry :
params.entrySet()) {
//
sb.append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue(),
encoding));
// sb.append("&");
// }
// //组装完成的数据
// data = sb.deleteCharAt(sb.length() -
1).toString();
// }
//转换为json样子
if(params!=null){
JSONObject n=new
JSONObject(params);
data="data="+URLEncoder.encode(n.toString(),"UTF-8");//特别强调,这里的设置时为了防止乱码
}
//传入请求的路径
URL url = new URL(Connection.baseUrl+requestUrl);
//打开连接
HttpURLConnection httpConn = (HttpURLConnection)
url.openConnection();
// 设置提交方式
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
httpConn.setRequestMethod("POST");
// post方式不能使用缓存
httpConn.setUseCaches(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
// 维持长连接
httpConn.setRequestProperty("Connection", "Keep-Alive");
// 设置浏览器编码
httpConn.setRequestProperty("Charset", "UTF-8");
httpConn.setRequestProperty("contentType", "UTF-8");
httpConn.setRequestProperty("Accept-Charset", "UTF-8");
DataOutputStream dos = new
DataOutputStream(httpConn.getOutputStream());
// 将请求参数数据向服务器端发送
dos.writeBytes(data);
dos.flush();
dos.close();
//接受数据的工作
InputStream inputStream=null;
if (httpConn.getResponseCode() == 200) {
// 获得服务器端输出流
inputStream=httpConn.getInputStream();
byte[] reserveData = readStream(inputStream);
result=new String(reserveData);
return result;
}
return null;
}
//通过输入流获得字节数组
public static byte[] readStream(InputStream is) throws
Exception {
byte[] buffer = new byte[1024];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int len = 0;
while((len=is.read(buffer)) != -1){
bos.write(buffer, 0, len);
}
is.close();
return bos.toByteArray();
}
}
服务端处理:
//客户端登陆方法
public ActionForward telUserLoging(HttpServletRequest
request,
HttpServletResponse response, ActionMapping mapping)
throws Exception {
this.reserJson=this.reserveJsongData(request);
JSONObject json=new JSONObject();
if(reserJson.get("account").equals("wang")&&reserJson.get("pwd").equals("123")){
json.put("accountId", "success");
}else{
json.put("accountId", "faile");
}
return gotoAndroidClient(request, json);
}
//最后的跳转方法————公用
public ActionForward gotoAndroidClient(HttpServletRequest
request,JSONObject json){
request.setAttribute("resultStr", json.toString());
return new ActionForward("/forandroid/result.jsp",
false);
}
//最前的接收数据方法————公用
public JSONObject reserveJsongData(HttpServletRequest
request){
String data=request.getParameter("data");
return JSONObject.fromObject(data);
}