云存储:
上一节我们介绍了怎么实现云检索的两种方式(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"));