android网络编程

所需积分/C币:10 2012-08-09 19:30:50 1.89MB PDF
3
收藏 收藏
举报

android网络编程
username": jie, password:111,email":172as ina com] user name:yong."password: 1232, emai l":Isai qa coTF JSONArray jsonArray=new JSONArray o JsON]b ject json-new JSONOb ject( 22 on put("password,123) son put( email".1720gg com): JSON]bject json 1=new JSONOb ecto son1 put C,jie) on1. put (pass word,111") son1. put(emai",1720s ina. com JoNb ject json 2=new JSoNOb ecto son put(user name","yong ): 12. wut( wass word :1232) son put ("email",Isadqg CoT"): sonAr ay. add(o, json, sondra ay. add(1, json) sonArray. add (2, json) JSON]b ject alOb ject=new JSONOE ject alob ject put(people, jsonArray): 来[“ progr ammer s" [" username":"ming,":123, i l":1720qg, com" 4 [username":"jie", "password": 111,"email":"1720s ina. com" [username":yong, password":1232",email":1saiqg comI 4 authors: [username":" ming,"Fa3sword":123."genre:science fiction"t [username":"jie, password:111,"genre":fantasy username",yong"password".1232",genre":christ ian fiction"I tuscans I"":"ming, "Password":123","instrument.guitar 1 [username:jie", password:111",instrument".piano] username: yong password:1232",instrument:flute F] JSoNArray jsonArr ay= new JSONArray o JSON]b ject json=new jSONObject () son put( username,ming) son put("password, 123) json. put (email.172iqq com" JSoNbject json 1=new JSONOb_ect(: sonl. pul Username son1 put ("pass,111) son1 put (email,172sina com") JsoNJb ject json2-=new JSONOb ect( son put(user name,yong) son put("pass word,1232) put( email","1sadqg co) IsonArray. add (o, json) jsondrray. add (1, json 1) ray. add (2, json 2) JSoNArray jsonarray 2=new jSoNarray( JsONObject json20-new JSONOb ject O: son2D. put(username","ming"): son20. put( pas sword,123) son20. put( genre, science fiction) JsoNOb ject json21=new JSONOb ject O son21 put(username,jia) son21, put( password, 111) JsoNJb ject json22=new uSONOb ject o son22. put( username."yon.) json22. put("password, 1232) son22. put( genre,"chr istar fiction) jsonArray2, add (0, json20 sonArray2 add(1, json21) sonArray2 add (2. json22) SONAr ray jsunArray3-IIew JSONAr ray JsONObject json30-new JSONObject o: //∥∥∥∥∥∥∥∥∥∥∥∥∥ son30. put("username,"minge son30. put( pas sword, 123) json30. put( instr ument, guitar JsoNJb ject json31=new JSONob ject O son31. put(username."jie) son. put( password,111 Gon31. put( instrument,"piano ): jsoNJb ject json 32=new JSONOb ject o son32. put( username"."yong): ison32. put("password, 1232) son32, put( instrument",flute): //iii sonArray3. add(O. json 30) sonArray3 add(1, json 31) sonArray3. add ( son JSON]b ject al Ob ject=new JSONOE ject o alObjeut. put(progr aimer s jsorIArt ay) alObject. put(author". jsonArray 2) ct. put (mus i CLans jsorArray3 yong: 1232: 1saiqg, com 获取任意节点的值:试例,第个节点,第三个子节点 jsoNarr ay isonarr aytl1 (JSONArray) alobject get( programmers y JSUN Jb ject jsonUb ject11=(SLINCb ject)sonaR rayt11 get (2) String username=jsonOb jectI l getstr ing("username) String password=jsonOb jectI 1. getStr ing(password); String emai l= jsonOb ject1l. getstr ing(emai") StringBuffer sUffer=new Str irgBuffer O sBuffer, append (user name+:). append (pas sword+: ) append (eima response. geLWr i Ler O wri Le ( usErname. loSLring O) 4 JSoNObject jsonenew JSONOb ject o: json put ("login,"login) response setContent Type( text/htmI; char set=utf-8) System out. pr intIn(json byte[ jsonBy tes s tringo getBytes utf * response. set ContentLength ( ison Bytes length) a response. getOutputstreamD) write (isonBy tes) JSONOb ject jsonFnew JSONOb, ect O: jso, put("log","login): //∥ byte l jsonBytes= json. toString O. getBy tes(utf-8): response, setcontentType( text html i charset=utf-8) response. setcontentLength jsonBytes, length response. getOutputStream O write (json Bytes): response. getoutputStream o. flush(; response. getoutputstream o. close( I catch(Exception e) I c pr intsteckTracco // return nul I: 我下面简单说一下JsON解析过程 通过访问htp:/ localhost:888/ Android ServerApp/login. action,得到如下JSON数据 JSONObject json=new JSONObjecto) ∥【这里在JsON中包含一个Map】 Map map=new HashMap<Object, String>O map, put("username",xiaoming map. put("password", 1234) map. put("state","1); json. put(userbean", map response. getWriter(). write(json. toString) 服务器端的配置完成了 下面我来配置 android客户端了。 由于 Android内置提拱了解析JSON数据的包。所以就不需要使用第三方包了 Android访问网络资源的代码如卜所示 view plaincopy to cl ipboardpr int? privatestaticStringuri=http://10.0.2.2:8888/androidservera pp/login. action getPDAServer Data (ur I) pr ivate void getPDAServer Data(Str ing ur I)[ Httpclientclient=newDefaulthttpcliento; //提拱默认的 Httpcl ient实现 Httppost request try I request = new Httppost(new Uri (urd)) Httpresponse response = client execute(request) //判断请求是否成功 if (response. getstatusL ine ( getstatus Code o= 200) //200表示请求成功 Httpentity entity=response. getentity o; f (entity nul l)I Str ing out= EntityUti ls tostr ing(entity) JSONOb ject jsonOb ject Str ing username Str ing password Str ing states User Bean user Bean=new User Bean O try //tuserbean":["username":100196","pass rd":"1234453"," State":1}} //JSONObject jsonOb ject new JSONOb jec (bui lder. tostr ing().getusoNOb ject( userbean") jsonOb ject new JSoNOb ject(out). getJSON Object(user bean") user Bean setUsername(jsonOb ject getstr in g(username)) user Bean. setPas sword( jsonOb ject getstr ng( password ) user Bean setState(Integer. par sent(sono bject getstr ing(state))); catch (JSONExcept ion e) / TODO Auto-generated catch block e. printstackTraceo new AlertDialog Bui I der(this). setMessage( user Bean. getUsername(+:+user B ean getState o). create(. showo I catch (URI SyntaxException e e. printstackTraceo new Aler tDialog Bui lder(this). setMessage(e. getMessag e). create O show o; catch (Cl ientProtoco l Exception e)[ e printstackTrace ( new Aler tDial og Bui lder (this). setMessage(e. getMessag eo). create O show o I catch(IOException e)I e pr intstackTrace) new AlertDialog. Bui lder(this). set Message(e. getMessag eo). create show O 里面的P地址:10.0.2.2,代表着手机模拟器本地的地址,相当于 localhost,但 不能使用 localhost或者127.0.0.1,因不是在PC机上测试,而是在手机AVD 上测试之。 而 UserBean. java,它就是一个 JAVABEAN,代码如下所示: view plaincopy to cl ipboardpr int? package po public class User Bean I pr ivate Str ing username pr ivate str ing password private int state, publ ic Str ing getUsernameo[ return username publ ic void setUsername(Str ing username)[ this. user name username public string getPassword o return password publ ic void setPassword (Str ing password)[ this password pa publ ic int gesTate[ return state ubl ic void setstate int state) this state state: 注意必须 Android manifest. xm中加入如下代码,不然 Android就不能访问web 资源 view plaincopy to clipboardpr int? Kuses-permi ss ion android: name="android. permission. INTERNET"/> 最终得到如下男面 t 5554: Android2 2.1 721AM MENU xiaoming. 1 ×少 6^7a89(0 Q E R SDFGH KL 合zxcB ALT SYM@ /2, ALT

...展开详情
试读 8P android网络编程
立即下载 身份认证后 购VIP低至7折
一个资源只可评论一次,评论内容不能少于5个字
您会向同学/朋友/同事推荐我们的CSDN下载吗?
谢谢参与!您的真实评价是我们改进的动力~
关注 私信
上传资源赚钱or赚积分
最新推荐
android网络编程 10积分/C币 立即下载
1/8
android网络编程第1页
android网络编程第2页

试读结束, 可继续读1页

10积分/C币 立即下载