百度——LBS.云 v2.0——云存储的POI创建和删除
云存储接口功能:支持单个geotable亿级数据及数百G大小的存储,每天支持百万量级读写,每秒支持万量级读写。
有了LBS云,个人开发LBS应用就非常方便了。今天研究了一下。在android上如何提交和删除POI数据。
主要操作就是HTTP的GET和POST。使用postman的chrome插件可以辅助调试。
涉及的主要技术:json,Http GET,Http Post
官方帮助页面
http://developer.baidu.com/map/lbs-geodata.htm
package baidumapsdk.demo;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.util.ArrayList;import java.util.List;import org.apache.http.HttpResponse;import org.apache.http.NameValuePair;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.client.params.CookiePolicy;import org.apache.http.client.params.HttpClientParams;import org.apache.http.entity.StringEntity;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.message.BasicNameValuePair;import org.apache.http.params.BasicHttpParams;import org.apache.http.params.HttpConnectionParams;import org.apache.http.params.HttpParams;import org.apache.http.protocol.HTTP;import org.apache.http.util.EntityUtils;import org.json.JSONArray;import org.json.JSONObject;import android.app.AlertDialog;import android.app.Dialog;import android.util.Log;public class KKHttpUtils {public final static String BD_LBS_CLOUD = "http://api.map.baidu.com/geodata/v2";// POSTpublic final static String BD_LBS_CLOUD_TABLE_CREATE_API = BD_LBS_CLOUD + "/geotable/create";public final static String BD_LBS_CLOUD_TABLE_UPDATE_API = BD_LBS_CLOUD + "/geotable/delete";public final static String BD_LBS_CLOUD_TABLE_DELETE_API = BD_LBS_CLOUD + "/geotable/update";// GETpublic final static String BD_LBS_CLOUD_TABLE_DETAIL_API = BD_LBS_CLOUD + "/geotable/detail";public final static String BD_LBS_CLOUD_TABLE_LIST_API = BD_LBS_CLOUD + "/geotable/list";public final static String BD_LBS_CLOUD_COLUMN_CREATE_API = BD_LBS_CLOUD + "/column/create";public final static String BD_LBS_CLOUD_COLUMN_DETAIL_API = BD_LBS_CLOUD + "/column/detail";public final static String BD_LBS_CLOUD_COLUMN_UPDATE_API = BD_LBS_CLOUD + "/column/update";public final static String BD_LBS_CLOUD_COLUMN_DELETE_API = BD_LBS_CLOUD + "/column/delete";public final static String BD_LBS_CLOUD_COLUMN_LIST_API = BD_LBS_CLOUD + "/column/list";public final static String BD_LBS_CLOUD_POI_CREATE_API = BD_LBS_CLOUD + "/poi/create";public final static String BD_LBS_CLOUD_POI_DETAIL_API = BD_LBS_CLOUD + "/poi/detail";public final static String BD_LBS_CLOUD_POI_UPDATE_API = BD_LBS_CLOUD + "/poi/update";public final static String BD_LBS_CLOUD_POI_DELETE_API = BD_LBS_CLOUD + "/poi/delete";public final static String BD_LBS_CLOUD_POI_LIST_API = BD_LBS_CLOUD + "/poi/list";private static KKHttpUtils uniqueInstance = null; private KKHttpUtils() { // Exists only to defeat instantiation. } public static KKHttpUtils getInstance() { if (uniqueInstance == null) { uniqueInstance = new KKHttpUtils(); } return uniqueInstance; } public static List<BasicNameValuePair> BuildPostListTable(){ KKHttpParams.clear_params(); KKHttpParams.add_params("ak", "28be793ffbf2f5a83ec846c97d325a85"); return KKHttpParams.to_post_list(); } public static HttpParams BuildGetListTable(){ HttpParams httpParameters = new BasicHttpParams(); httpParameters.setParameter("ak", "28be793ffbf2f5a83ec846c97d325a85"); return httpParameters; } public int dealwithresp(String resp){ int TIMEOUTSPAN = 5000; int liret = -1;try {JSONObject jsonObject = new JSONObject(resp);String statuscode = jsonObject.getString("status");String statusmsg = jsonObject.getString("message");liret = Integer.parseInt(statuscode);Log.v("KKHP", resp);// 0:成功if (0 == liret) {JSONArray jsonGeoTables = jsonObject.getJSONArray("geotables");for (int i = 0; i < jsonGeoTables.length(); i++) {JSONObject jsonitem = (JSONObject) jsonGeoTables.opt(i);String tableid = jsonitem.getString("id");String tablename = jsonitem.getString("name");String tablegeotype = jsonitem.getString("geotype");String tablecreatetime = jsonitem.getString("create_time");String tableispublished = jsonitem.getString("is_published");}}} catch (Exception e) {// TODO: handle exceptionLog.v("KKHP", "except/"+e.toString());}return liret; } public int dealwithpoiresp(String resp){ int TIMEOUTSPAN = 5000; int liret = -1;try {JSONObject jsonObject = new JSONObject(resp);String statuscode = jsonObject.getString("status");String statusmsg = jsonObject.getString("message");liret = Integer.parseInt(statuscode);Log.v("KKHP POI", resp);// 0:成功if (0 == liret) {String size = jsonObject.getString("size");String total = jsonObject.getString("total");JSONArray jsonGeoTables = jsonObject.getJSONArray("pois");for (int i = 0; i < jsonGeoTables.length(); i++) {JSONObject jsonitem = (JSONObject) jsonGeoTables.opt(i);String tableid = jsonitem.getString("id");String tablegeotableid = jsonitem.getString("geotable_id");String tabletitle = jsonitem.getString("title");String tableaddress = jsonitem.getString("address");String tablecreatetime = jsonitem.getString("create_time");String tablelng = jsonitem.getString("longtude");String tablelat = jsonitem.getString("latitude");}}} catch (Exception e) {// TODO: handle exceptionLog.v("KKHP", "except/"+e.toString());}return liret; } public String getdata(String url){ int TIMEOUTSPAN = 30000; String resp=""; HttpGet localHttpGet = new HttpGet(url); try {HttpResponse localHttpResponse;// 实际测试设置超时,读取效果更差。//HttpParams httpParameters = new BasicHttpParams();//HttpConnectionParams.setConnectionTimeout(httpParameters, TIMEOUTSPAN);//HttpConnectionParams.setSoTimeout(httpParameters, TIMEOUTSPAN); DefaultHttpClient localDefaultHttpClient = new DefaultHttpClient();// Compile Web or will happen invalid cookieHttpClientParams.setCookiePolicy(localDefaultHttpClient.getParams(), CookiePolicy.BROWSER_COMPATIBILITY); localHttpResponse = localDefaultHttpClient.execute(localHttpGet);resp = EntityUtils.toString(localHttpResponse.getEntity(), HTTP.UTF_8);}catch (Exception e) {// TODO Auto-generated catch block//e.printStackTrace();Log.v("KKHP", "except/"+e.toString());} return resp; } public int postdata(String url, List<BasicNameValuePair> params){int liret = -1; DefaultHttpClient localDefaultHttpClient = new DefaultHttpClient(); //HttpPost localHttpPost = new HttpPost("http://www.1zpark.com/android/parkadd.php"); HttpPost localHttpPost = new HttpPost(url); try {//localHttpPost.setEntity(new UrlEncodedFormEntity(_params, "UTF-8")); localHttpPost.setEntity(new UrlEncodedFormEntity(params));// Log.v("KKHP", params.size() +"/"+localHttpPost.getParams().toString()); HttpResponse localHttpResponse = localDefaultHttpClient.execute(localHttpPost); String resp = EntityUtils.toString(localHttpResponse.getEntity()); JSONObject jsonObject = new JSONObject(resp); Log.v("KKHP", resp);// {// "status": 2,// "message": "ak:参数必需"// } String statuscode = jsonObject.getString("status"); String statusmsg = jsonObject.getString("message"); liret = Integer.parseInt(statuscode); // 0:成功 if(0 == liret){ } } catch (Exception e) {// TODO Auto-generated catch block//e.printStackTrace(); Log.v("KKHP", "except/"+e.toString());} return liret;}}