Android客户端与服务器之间传递json数据
来源:互联网 发布:arp通过ip获取mac 编辑:程序博客网 时间:2024/05/18 01:10
在服务器与客户端之间通信,json数据是一种常用格式,本文主要在服务器端构建数据,在客户端接收显示,并且在listview上显示出来
服务器端的构建
简单的javabean与返回结果函数与插入函数略过
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); PrintWriter out = response.getWriter(); List<MyShop> shops = JsonService.getListShop(); StringBuffer sb = new StringBuffer(); sb.append('['); for (MyShop shop : shops) { sb.append('{').append("\"name\":").append("\""+shop.getName()+"\"").append(","); sb.append("\"detail\":").append("\""+shop.getDetail()+"\"").append(","); sb.append("\"distance\":").append("\""+shop.getDistance()+"\"").append(","); sb.append("\"address\":").append("\""+shop.getAddress()+"\"").append(","); sb.append("\"popularity\":").append(shop.getPopularity()); sb.append('}').append(","); } sb.deleteCharAt(sb.length() - 1); sb.append(']'); out.write(new String(sb)); out.flush(); out.close(); }
在浏览器中直接输入访问http://localhost:8080/AppServer/JsonServlet
可得
可以在服务器端直接查看json数据
客户端接收与解析json数据
public class JsonParse { /** * 解析Json数据 * * @param urlPath * @return mlists * @throws Exception */ public static List<MyShop> getListShop(String urlPath) throws Exception { List<MyShop> mlists = new ArrayList<MyShop>(); byte[] data = readParse(urlPath); JSONArray array = new JSONArray(new String(data)); for (int i = 0; i < array.length(); i++) { JSONObject item = array.getJSONObject(i); String name = item.getString("name"); String detail = item.getString("detail"); String distance = item.getString("distance"); String popularity = item.getString("popularity"); String address = item.getString("address"); mlists.add(new MyShop(name, detail, distance,address,popularity)); } return mlists; } /** * 从指定的url中获取字节数组 * * @param urlPath * @return 字节数组 * @throws Exception */ public static byte[] readParse(String urlPath) throws Exception { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] data = new byte[1024]; int len = 0; URL url = new URL(urlPath); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); InputStream inStream = conn.getInputStream(); while ((len = inStream.read(data)) != -1) { outStream.write(data, 0, len); } inStream.close(); return outStream.toByteArray(); }}
在activity中开启子线程来接收服务器数据
new Thread(new Runnable() { private String tag; @Override public void run() { // TODO Auto-generated method stub //获得新闻集合 List<MyShop> shopList = null; try { shopList = JsonParse.getListShop("http://192.168.247.1:8080/AppServer/JsonServlet"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } Log.i(tag, "在RUN中LIST长度为"+shopList.size()); Message msg=new Message(); if(shopList!=null) { msg.what=SUCCESS; msg.obj=shopList; }else { msg.what=FAILED; } handler.sendMessage(msg); Log.i(tag, "***********T长度为"+shopList.size()); } }).start();
消息处理器
//消息处理器 private Handler handler=new Handler(){ /** * 接收消息 */ @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub String tag = null; switch (msg.what) { case SUCCESS: //访问成功,有数据 //绑定数据 Log.i(tag,"%%%%%%%%%%到达了消息处理器"); myshoplist=(List<MyShop>) msg.obj; Log.i(tag, "handleMessage中数据newInfoList长度为"+myshoplist.size()); NearAdapter adapter=new NearAdapter(); Log.i(tag, "有没有到达ADAPTER"+adapter.getCount()); showList.setAdapter(adapter); break; case FAILED: //访问失败 Toast.makeText(ChooseMer.this, "当前网络崩溃了", 0).show(); break; default: break; } } };
配置适配器
public class NearAdapter extends BaseAdapter { @Override public int getCount() { // TODO Auto-generated method stub return myshoplist.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { LayoutInflater inflater = LayoutInflater .from(ChooseMer.this); convertView = inflater.inflate(R.layout.nearby_list_item, null); init(convertView,position); } return convertView; } public void init(View convertView,int position) { hold.name = (TextView) convertView .findViewById(R.id.nearby_item_name); MyShop shop=myshoplist.get(position); hold.name.setText(shop.getName()); hold.local = (TextView) convertView .findViewById(R.id.nearby_item_local); hold.local.setText(shop.getDetail()); hold.dis1 = (TextView) convertView .findViewById(R.id.nearby_item_dis1); hold.dis1.setText(shop.getDistance()); hold.dis2 = (TextView) convertView .findViewById(R.id.nearby_item_dis2); hold.dis2.setText(shop.getAddress()); hold.dis3 = (TextView) convertView .findViewById(R.id.nearby_item_dis3); hold.dis3.setText(shop.getPopularity()); } }
配置完成,效果如下
0 0
- Android客户端与服务器之间传递json数据
- Android开发:客户端与服务器通过传递和接收json数据进行交互
- Json数据的前端与服务器之间的传递
- 实践Android客户端与服务端之间使用JSON交互数据。
- Android与服务器间数据传递及JSON解析
- Android 使用Json实现服务器与客户端数据的交互
- Struts2服务器和Android客户端利用JSON进行数据的传递
- android跟服务器使用json传递数据
- HttpClient模拟客户端向 服务器接口 传递 Json 数据
- ssh配置客户端和服务器的交互json传递数据
- Android 对Android客户端与服务端传递的Json数据进行加密、解密操作
- wcf 客户端与服务器之间在传递DataTable问题
- 【Android 开发】: Android客户端与服务端之间使用JSON交互数据。
- Android客户端与服务器之间的通信
- json在服务器与客户端之间传输的一篇文章
- Android-Activity与Fragment之间传递数据
- 页面之间json数据传递
- JQuery与一般处理程序之间传递JSON数据
- Unix timestamp时间 与 普通时间 转换方法
- button 文字靠左 内容偏移
- HTTP长连接与短连接
- 归并排序
- hadoop的应用场景分析
- Android客户端与服务器之间传递json数据
- unity camera随机点拍摄并能缓动跟随目标
- Oracle EBS R12 WIP Component Issue&Return Process
- 模态框的的实现
- VMware下配置Linux IP,解决Linux 和主机互ping问题
- 三种数据库的分页
- 如何用 Retrofit 2 在安卓上实现 HTTP 访问?
- Python Mako
- eclipse 导入其他人的项目时tomcat出现Hibernate错误