Commit 8e9f813c by cuiliang.shi

超时时间 15秒

1 parent 76f67bbc
...@@ -314,24 +314,10 @@ public class BirdService extends Service { ...@@ -314,24 +314,10 @@ public class BirdService extends Service {
private void postLocationInfo() { private void postLocationInfo() {
JSONObject jsonBody = new JSONObject(); new Thread(new Runnable() {
try { @Override
jsonBody.put("longitude", mLongitude); public void run() {
jsonBody.put("latitude", mLatitude); final StringBuffer strbuf = new StringBuffer();
jsonBody.put("time", mStartTime);
jsonBody.put("meid", mIMEIStr);
jsonBody.put("tel", mMobile);
jsonBody.put("iccid", mICCIDStr);
jsonBody.put("imsi", mIMSIStr);
jsonBody.put("model", mModel);
jsonBody.put("cfgv", ret_cfgv);
jsonBody.put("gcs", "GCJ02");
} catch (JSONException x) {
x.printStackTrace();
}
Log.d(TAG, "url: " + mPath + ", jsonBody: " + jsonBody.toString());
/* StringBuilder strbuf = new StringBuilder();
strbuf.append("longitude=" + mLongitude); strbuf.append("longitude=" + mLongitude);
strbuf.append("&latitude=" + mLatitude); strbuf.append("&latitude=" + mLatitude);
strbuf.append("&time=" + mStartTime); strbuf.append("&time=" + mStartTime);
...@@ -344,25 +330,49 @@ public class BirdService extends Service { ...@@ -344,25 +330,49 @@ public class BirdService extends Service {
strbuf.append("&gcs=" + "GCJ02"); strbuf.append("&gcs=" + "GCJ02");
Log.d(TAG, "REQUEST: URL: " + mPath + ", BODY:" + strbuf); Log.d(TAG, "REQUEST: URL: " + mPath + ", BODY:" + strbuf);
IotApi.get(mPath + "?" + strbuf.toString(), new IotNetApiCallback(){*/
IotApi.postLocationInfo(mPath, jsonBody , new IotNetApiCallback() { URL url = null;
@Override HttpURLConnection httpURLConnection = null;
public void onSuccess(String response) { OutputStream os = null;
Log.d(TAG, "server response: " + response); try {
mHasSendTheLocationInfo = true; url = new URL(mPath);
JsonObject resp = new JsonParser().parse(response).getAsJsonObject(); } catch (Exception e) {
e.printStackTrace();
ret_code = resp.get("ret_code").getAsInt(); }
try {
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setReadTimeout(15000);
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestProperty("Charset", "UTF-8");
httpURLConnection.setRequestProperty("Content-Length", strbuf.toString().length() + "");
os = httpURLConnection.getOutputStream();
os.write(strbuf.toString().getBytes());
os.flush();
int responseCode = httpURLConnection.getResponseCode();
Log.d(TAG, "responseCode--" + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStreamReader is = new InputStreamReader(httpURLConnection.getInputStream());
StringBuffer sb = new StringBuffer();
int i = 0;
while ((i = is.read()) != -1) {
sb.append((char) i);
}
is.close();
Log.d(TAG, "server response: " + sb);
JSONObject jsonObject = new JSONObject(sb.toString());
ret_code = jsonObject.getInt("ret_code");
if (ret_code == 1){ if (ret_code == 1){
ret_cfgv = resp.get("cfgv").getAsString(); ret_cfgv = jsonObject.getString("cfgv");
mPath = resp.get("url").getAsString(); mPath = jsonObject.getString("url");
Log.d(TAG, "new path: " + mPath); Log.d(TAG, "new path: " + mPath);
if (!mPath.equals("0")){ if (!mPath.equals("0")){
DevicePreferences.getInstance(mContext).putString("bird_location_upload_url", mPath); DevicePreferences.getInstance(mContext).putString("bird_location_upload_url", mPath);
} }
int timeSpanMinutes = resp.get("time_span").getAsInt(); int timeSpanMinutes = jsonObject.getInt("time_span");
if (timeSpanMinutes == 999999){ if (timeSpanMinutes == 999999){
DevicePreferences.getInstance(mContext).putLong("bird_location_upload_interval", 0); DevicePreferences.getInstance(mContext).putLong("bird_location_upload_interval", 0);
}else if (timeSpanMinutes == 0){ }else if (timeSpanMinutes == 0){
...@@ -379,6 +389,10 @@ public class BirdService extends Service { ...@@ -379,6 +389,10 @@ public class BirdService extends Service {
Log.d(TAG, "upload failed"); Log.d(TAG, "upload failed");
} }
} else if (responseCode != 200) {
Log.d(TAG, "request failed");
}
setAlarm(); setAlarm();
// close gps & network. // close gps & network.
...@@ -386,21 +400,29 @@ public class BirdService extends Service { ...@@ -386,21 +400,29 @@ public class BirdService extends Service {
intent.addFlags(0x01000000); intent.addFlags(0x01000000);
getApplicationContext().sendBroadcast(intent); getApplicationContext().sendBroadcast(intent);
} } catch (Exception e) {
Log.d(TAG, "Exception:" + e.toString());
@Override
public void onFailed(Exception e) {
Log.d(TAG, "onFailed Exception:" + e.toString());
setAlarm(); setAlarm();
/*mErrorCount++; mErrorCount++;
if (mErrorCount > 5){ if (mErrorCount > 5){
setAlarm(); setAlarm();
}else{ }else{
long temp = DevicePreferences.getInstance(mContext).getLong("bird_location_upload_interval", DEFAULTINTERVAL); long temp = DevicePreferences.getInstance(mContext).getLong("bird_location_upload_interval", DEFAULTINTERVAL);
mErrorHandler.sendEmptyMessageDelayed(0, temp); mErrorHandler.sendEmptyMessageDelayed(0, temp);
}*/
} }
}); } finally {
httpURLConnection.disconnect();
Log.d(TAG, "close connect");
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}).start();
} }
private void initLocation(Context context) { private void initLocation(Context context) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!