• 高德云存储


    云存储:

    上一节我们介绍了怎么实现云检索的两种方式(1.http协议,2.android平台sdk)

    今天我们介绍云存储操作(增删改)

    定义URL:

        // CREATE
        public final static String CREATE_TABLE = "http://yuntuapi.amap.com/datamanage/table/create?";
        // QUERY
        public final static String LOCAL_QUERY = "http://yuntuapi.amap.com/datasearch/local?";
        public final static String AROUND_QUERY = "http://yuntuapi.amap.com/datasearch/around?";
        public final static String POLYGON_QUERY = "http://yuntuapi.amap.com/datasearch/polygon?";
        public final static String ID_QUERY = "http://yuntuapi.amap.com/datasearch/id?";
        // INSERT
        public final static String INSERT = "http://yuntuapi.amap.com/datamanage/data/create?";
        public final static String INSERT_BATCH_= "http://yuntuapi.amap.com/datamanage/data/batchcreate?";
        public final static String INSERT_BATCH_IMPORTSTATUS = "http://yuntuapi.amap.com/datamanage/batch/importstatus?";
        // UPDATE
        public final static String UPDATE = "http://yuntuapi.amap.com/datamanage/data/update?";
        // DELETE
        public final static String DELETE = "http://yuntuapi.amap.com/datamanage/data/delete?";

    创建TABLE:

    public List<NameValuePair> getPostDatas(String tableName) {
            List<NameValuePair> formparams = new ArrayList<NameValuePair>();
            formparams.add(new BasicNameValuePair("key", HTTPUtils.WEBAPIKEY));
            formparams.add(new BasicNameValuePair("name", tableName));
            return formparams;
        }     
         
    public void doPost() {
            try {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(CREATE_TABLE);
                httppost.addHeader("Content-Type", "application/x-www-form-urlencoded");
                httppost.setEntity(new UrlEncodedFormEntity(getPostDatas("newTable"), "UTF-8"));
                HttpResponse response = httpclient.execute(httppost);
                int code = response.getStatusLine().getStatusCode();
                if (code == 200) {
                    String recieve = EntityUtils.toString(response.getEntity());
                    System.out.println(recieve);
                }
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    插入数据:

    private final static String[] LOCATION_FIELD = new String[]{"_id","_name","_address"}; 
         
    public List<NameValuePair> insertData(String tableId, String[] datas) {
            try {
                JSONObject data = new JSONObject();
                for (int i = 1; i < LOCATION_FIELD.length; i++)
                    data.put(LOCATION_FIELD[i], datas[i - 1]); 
                return toNameValuePairData(data.toString(), tableId);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }
        
    private List<NameValuePair> toNameValuePairData(String data,String tableid ){
            List<NameValuePair> formparams = new ArrayList<NameValuePair>();
            formparams.add(new BasicNameValuePair("key", WEBAPIKEY));
            formparams.add(new BasicNameValuePair("tableid",tableid));
            formparams.add(new BasicNameValuePair("loctype", "2"));
            formparams.add(new BasicNameValuePair("data", data.toString()));
            return formparams;
        } 
         
    public void doPost() {
            try {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(INSERT);
                httppost.addHeader("Content-Type", "application/x-www-form-urlencoded");
                httppost.setEntity(new UrlEncodedFormEntity(insertData("白羊沟自然风景区","北京市昌平区流村镇北禾路白羊沟自然风景区"), "UTF-8"));
                HttpResponse response = httpclient.execute(httppost);
                int code = response.getStatusLine().getStatusCode();
                if (code == 200) {
                    String recieve = EntityUtils.toString(response.getEntity());
                    System.out.println(recieve);
                }
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    更新数据:

    把上面的红色部分改一改就行。

    //1.INSERT -> UPDATE 红字部分 
    //2.添加updateData方法
    public List<NameValuePair> updateData(String tableId, String[] datas) {
            try {
                JSONObject data = new JSONObject();
                for (int i = 0; i < LOCATION_FIELD.length; i++)
                        data.put(LOCATION_FIELD[i], datas[i]);
          return toNameValuePairData(data.toString(), tableId); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } //3.更新红色部分方法调用 httppost.setEntity(new UrlEncodedFormEntity(updateData("1","白羊沟自然风景区","北京市昌平区流村镇北禾路白羊沟自然风景区"), "UTF-8"));

    删除数据:

    //1.INSERT -> DELETE 红字部分 
    //2.添加deleteData方法
    public List<NameValuePair> deleteData(String tableId, String id) {
            List<NameValuePair>  formparams = new ArrayList<NameValuePair>();
            formparams.add(new BasicNameValuePair("key", WEBAPIKEY));
            formparams.add(new BasicNameValuePair("tableid",tableId));
            formparams.add(new BasicNameValuePair("ids", id));
            return formparams;
        }
    
    //3.更新红色部分方法调用
    httppost.setEntity(new UrlEncodedFormEntity(deleteData("1"), "UTF-8"));
  • 相关阅读:
    【iCore4 双核心板_FPGA】例程八:乘法器实验——乘法器使用
    【iCore4 双核心板_ARM】例程十一:DMA实验——存储器到存储器的传输
    【iCore1S 双核心板_FPGA】例程八:触发器实验——触发器的使用
    【iCore4 双核心板_ARM】例程十:RTC实时时钟实验——显示时间和日期
    【iCore4 双核心板_ARM】例程九:ADC实验——电源监控
    WebBrowser的Cookie操作之流量刷新机
    网站受攻击的常用手段
    八爪鱼招标网的百度权重升为2了,独立IP也从0快速发展为1000
    网站添加数据出错,原来是MS SQL Server2008日志文件占据空间过大导致的
    当你的网站被疯狂攻击时你能做什么?
  • 原文地址:https://www.cnblogs.com/hongguang-kim/p/5720945.html
Copyright © 2020-2023  润新知