Android-Iphone运用Http协议使用Json格式进行服务器端和客户端通信

[color=green]测试类pojo和json打太极[/color]


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;
/**
* JSON对象和pojo的互相转换
*
* 运用的ezmorph-1.0.2.jar
* @author myth
*
*/
public class JSONTestForStruts {

/**
* 将普通的pojo(Plain Old Java Objects)转换成JSON字符串
* @return
*/
public JSONObject bean2json() {
User user = new User();
user.setId("JSONTest");
user.setName("JSONTest");
user.setPassword("JSON");
user.setSay("Hello,i am JSONTest.java");
JSONObject jsonObject = new JSONObject();

ArrayList<User> list=new ArrayList<User>();
list.add(user);
list.add(user);

Map<Integer, String> map = new HashMap<Integer, String>();
map.put(220180, null);
map.put(220181, "Json测试类");

jsonObject.accumulate("user", user);//accumulate累加
jsonObject.accumulate("list", list);
jsonObject.accumulate("map", map);
System.out.println("User转换成JSON格式的字符串:"+jsonObject.toString());
return jsonObject;
}

/**
* 从JSONObject对象中反向解析出User对象
* @param jsonObject
*/
public void json2bean(JSONObject jsonObject) {
User user=(User)JSONObject.toBean((JSONObject)jsonObject.get("user"),User.class);
System.out.println("转换得到的User对象的Name为:"+user.getName());
}

public static void main(String[] s) {
JSONTestForStruts jsonTest=new JSONTestForStruts();
jsonTest.json2bean(jsonTest.bean2json());
}
}
[color=red] [size=large]在action中调用[/size][/color]
response.setContentType("text/html;charset=gbk");
PrintWriter out=response.getWriter();
//将要被返回到客户端的对象
JSONObject json=new JSONObject();
json.accumulate("success", true);
json.accumulate("esff", esff);
out.println(json.toString());
out.flush();
out.close();
展开阅读全文

HTTP协议通信问题

06-28

[code=C#]rnrn MyHttp mh = new MyHttp();rnmh.sUrl = "http://www.fj987.com/loginin.php";//rnmh.fSetMethodPost();rnmh.sHwrq.KeepAlive = true;rn rnCookieCollection myCookies = null;rnCookieContainer myCookieContainer = new CookieContainer();rnmh.sHwrq.CookieContainer = myCookieContainer;rnrnstring data = "entype=1&username=zlcp520&password=432503";rnmh.fWriteData(data);rnrnmh.fSendAndGet();rnstring cookieHeader = mh.sHwrq.CookieContainer.GetCookieHeader(new Uri(mh.sUrl)); rnmh.sHwrp.Close();rn//得到cookieHeader为ASPSESSIONIDSQAQDSBA=MGGODBHAEDDHOPGAFDLKCKDL rn//每次登录得到的cookieHeader都不同.rnrn//然后获取需要登录才能查看的页面rnMyHttp mh = new MyHttp();rnmh.sUrl = "http://www.fj987.com/main.html";//rnmh.sHwrq.KeepAlive = true;rnmh.sHwrq.Headers.Set("Cache-Control", "no-cache");rnmh.sHwrq.ContentType = "application/x-www-form-urlencoded";rnmh.sHwrq.Headers.Add("cookie:ASPSESSIONIDSQAQDSBA=MGGODBHAEDDHOPGAFDLKCKDL");rnmh.sHwrq.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 685; TencentTraveler 4.0; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";rnmh.sHwrq.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/vnd.ms-excel, application/msword, */*";rnmh.sHwrq.Headers.Set("Accept-Language", "zh-cn");rnmh.sHwrq.Headers.Set("Accept-Encoding", "gzip, deflate");rnmh.sHwrq.AllowAutoRedirect = true;rnrnstring s = mh.fGetSource(Encoding.GetEncoding("gb2312"));rnrn//获得s却显示你未登录rn[/code] 论坛

没有更多推荐了,返回首页