这是我的代码,总是先执行httpPostToUpdateMyLocation 然后再执行 httpCreatMyTable(); 和httpGetTableId(); 我的想法是 让 httpCreatMyTable();和httpGetTableId()先执行,得到geoTableID,但是最后还是先执行httpPostToUpdateMyLocation 请问该如何解决!
public class HttpHelper {
private HashMap paramMap;
private int geoTableID=-1;
private NotificationUtil mNotificationUtil; //该类用于通知网络的错误信息等
private RequestParams requestParams = null;
public long lastTimer; //用于记录上传的时间
private String MyAK;
private String imei ="-1";
public void httphelper(Location location,Context mContext){
mNotificationUtil =new NotificationUtil(mContext);
SharedPreferences sharedPreferences = mContext.getSharedPreferences(com.DoBest.constant.StringConstant.PREFS_NAME, Context.MODE_PRIVATE);
MyAK = sharedPreferences.getString("AK", com.DoBest.constant.StringConstant.DEF_AK);
// 获取手机设备号
TelephonyManager telephonyManager = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
imei = telephonyManager.getDeviceId();
//向服务器查询表格获得表格ID;
httpCreatMyTable(); //创建表格,首次运行
httpGetTableId(); //获得表格
httpPostToUpdateMyLocation(location); //传输数据
}
private void httpGetTableId() {
if (geoTableID != -1) return; //表示并不是第一次上传数据,已经获得表格的单号
HashMap paramTable = new HashMap();
paramTable.put("ak", MyAK);
paramTable.put("name", "staff");
requestParams = new RequestParams(paramTable); //设置请求的参数
LocationHttpClient.get("geodata/v3/geotable/list", requestParams,
new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, JSONObject response) {
try {
if (statusCode ==200) {
Log.i("httpGetTableId", "-------连接成功-----");
}
GpsLog.writeLogFile("查询web端表格:查询连接成功"+statusCode);
if (response.has("geotables")) {
JSONArray jsonArray = response.getJSONArray("geotables");
if (jsonArray.length() != 0) {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
if (jsonObject.has("name")) {
String name = jsonObject.getString("name");
System.out.println("name------->" + name);
if (name.equals("staff")) {
geoTableID = jsonObject.getInt("id");
System.out.println("id------->"+ geoTableID);
GpsLog.writeLogFile("查询web端表格:成功——id:"+ geoTableID);
break;
}
}
}
}
}
else if (geoTableID == -1) {
mNotificationUtil.creatDefaultNotification("未在服务器端找到表");
GpsLog.writeLogFile("查询web端表格:未找到");
}
}
catch (JSONException e) {
GpsLog.writeLogFile("查询web端表格:获取表格数据错误");
e.printStackTrace();
}
super.onSuccess(statusCode, response);
}
@Override
public void onFailure(Throwable e, JSONObject errorResponse) {
System.out.println("failure");
GpsLog.writeLogFile("查询web端表格:连接错误");
super.onFailure(e, errorResponse);
}
});
}
private void httpCreatMyTable() {
HashMap<String, String> paramTable = new HashMap<String, String>();
paramTable.put("ak", MyAK);
paramTable.put("name", "staff_" + imei);
paramTable.put("is_published", "1");
paramTable.put("geotype", "1");
requestParams = new RequestParams(paramTable); //设置请求的参数
LocationHttpClient.post("geodata/v3/geotable/create",requestParams,new JsonHttpResponseHandler()
{
@Override
public void onSuccess(int statusCode, Header[] headers,
org.json.JSONObject response){
super.onSuccess(statusCode, response);
try {
if (statusCode ==200) {
GpsLog.writeLogFile("创建web端表格:创建表格连接已经打开,并且获得响应"+statusCode);
}
if (response.has("message")) {
String message = "";
message = response.getString("message");
if (message.equals("成功")) {
GpsLog.writeLogFile("创建web端表格:初次成功在服务端创建表单");
} else if (message.equals("表的name重复")) {
GpsLog.writeLogFile("创建web端表格:error 表单重复");
}else {
GpsLog.writeLogFile("创建web端表格:"+response.getString("message"));
}
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
@Override
public void onFailure(int statusCode, Header[] headers,
String responseBody, Throwable e) {
// TODO Auto-generated method stub
GpsLog.writeLogFile("创建web端表格:网络连接错误");
mNotificationUtil.creatDefaultNotification("创建web端表格:网络连接错误");
super.onFailure(statusCode, headers, responseBody, e);
}
});
}
private void httpPostToUpdateMyLocation(Location location) {
GpsLog.writeLogFile("运行到上传"+geoTableID);
paramMap =new HashMap<String, String>();
if (geoTableID!=-1) { //获得对应的表格号,然后再上传参数到百度API中
Log.e("httpPostToUpdateMyLocation", "上传");
paramMap = new HashMap<String, String>();
paramMap.put("ak", MyAK); //将自己的钥匙传入
paramMap.put("geotable_id", geoTableID + "");
paramMap.put("time", System.currentTimeMillis() + "");
paramMap.put("coord_type", 1 + "");
paramMap.put("title", null);
GpsLog.writeLogFile(System.currentTimeMillis()+"");
if (location == null) {
mNotificationUtil.creatDefaultNotification("位置信息捕获错误");
GpsLog.writeLogFile("上传位置信息ERROR:上传位置信息捕获错误");
}
paramMap.put("longitude", location.getLongitude() + "");
paramMap.put("latitude", location.getLatitude() + "");
requestParams = new RequestParams(paramMap);
LocationHttpClient.post("geodata/v3/poi/create", requestParams,
new JsonHttpResponseHandler() {
@Override
public void onFailure(Throwable e,JSONObject errorResponse) {
Log.e("json-------------->", "upload onFailure");
mNotificationUtil.creatDefaultNotification("上传位置数据:上传数据失败");
super.onFailure(e, errorResponse);
}
@Override
public void onSuccess(int statusCode, Header[] headers,
org.json.JSONObject response) {
if (statusCode == 200) {
GpsLog.writeLogFile("上传位置数据:响应"+statusCode);
// 检查是否返回的是上传成功
try {
if (response.has("message")) {
String message = "";
message = response.getString("message");
GpsLog.writeLogFile(message);
GpsLog.writeLogFile("上传位置数据:"+message);
if (message.equals("成功")) {
mNotificationUtil.creatDefaultNotification("成功上传一个坐标.");
}
else {
mNotificationUtil.creatDefaultNotification("坐标上传失败.");
}
}
else {
mNotificationUtil.creatDefaultNotification("坐标上传失败.");
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
mNotificationUtil.creatDefaultNotification("坐标上传失败.");
}
lastTimer = System.currentTimeMillis();
}
});
}
}
}