iMap4Android开发包简介(一)

为了简化Android和服务器的数据交换,开发了iMap4Android和iMap4Server

对于开发人员来讲,只需要与Java Class打交道,无需了解任何Json知识

一、Android从服务器获取数据 ,很简单


  URL url = new URL("http://10.25.36.56:9080/bpmsx/images/legend.json");
   ArrayList<TreeNode> aTree = json.readValue(url, ArrayList.class);

 

获取的数据保存在aTree中

 

二、Android向服务器传送数据 ,很简单

json.writeValue(stream, aTree);

 

三、服务器端开发同样简单

 protected void doProcess(HttpServletRequest request,
   HttpServletResponse response) throws ServletException, IOException {
 
  ArrayList<TreeNode> aTree = new ArrayList<TreeNode>();
  TreeNode aTreeRoot = new TreeNode();
  aTreeRoot.setId(-1);
  aTreeRoot.setText("所有图层");
  aTree.add(aTreeRoot);

  PrintWriter writer = response.getWriter();
  jsonMapper.writeValue(writer, aTree);
 }

 

Android完整代码如下:

package com.example.imaptest;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;

import com.baosight.imap.easyui.TreeNode;
import com.baosight.imap.json.databind.JsonMapper;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  JsonMapper json = new JsonMapper(); // can reuse, share globally

  ArrayList<TreeNode> aTree = new ArrayList<TreeNode>();

  URL url = null;
  try {
   url = new URL("http://10.25.36.56:9080/bpmsx/images/legend.json");
   ArrayList<TreeNode> aTree = json.readValue(url, ArrayList.class);
   json.writeValue(new File("/sdcard/legend.json"), aTree);
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值