Commit 8e9f813c by cuiliang.shi

超时时间 15秒

1 parent 76f67bbc
......@@ -314,93 +314,115 @@ public class BirdService extends Service {
private void postLocationInfo() {
JSONObject jsonBody = new JSONObject();
try {
jsonBody.put("longitude", mLongitude);
jsonBody.put("latitude", mLatitude);
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("&latitude=" + mLatitude);
strbuf.append("&time=" + mStartTime);
strbuf.append("&meid=" + mIMEIStr);
strbuf.append("&tel=" + mMobile);
strbuf.append("&iccid=" + mICCIDStr);
strbuf.append("&imsi=" + mIMSIStr);
strbuf.append("&model=" + mModel);
strbuf.append("&cfgv=" + ret_cfgv);
strbuf.append("&gcs=" + "GCJ02");
Log.d(TAG, "REQUEST: URL: " + mPath + ", BODY:" + strbuf);
IotApi.get(mPath + "?" + strbuf.toString(), new IotNetApiCallback(){*/
IotApi.postLocationInfo(mPath, jsonBody , new IotNetApiCallback() {
new Thread(new Runnable() {
@Override
public void onSuccess(String response) {
Log.d(TAG, "server response: " + response);
mHasSendTheLocationInfo = true;
JsonObject resp = new JsonParser().parse(response).getAsJsonObject();
ret_code = resp.get("ret_code").getAsInt();
if (ret_code == 1){
ret_cfgv = resp.get("cfgv").getAsString();
mPath = resp.get("url").getAsString();
Log.d(TAG, "new path: " + mPath);
if (!mPath.equals("0")){
DevicePreferences.getInstance(mContext).putString("bird_location_upload_url", mPath);
}
int timeSpanMinutes = resp.get("time_span").getAsInt();
if (timeSpanMinutes == 999999){
DevicePreferences.getInstance(mContext).putLong("bird_location_upload_interval", 0);
}else if (timeSpanMinutes == 0){
Log.d(TAG, "time span not change");
}else{
DevicePreferences.getInstance(mContext).putLong("bird_location_upload_interval", timeSpanMinutes);
}
public void run() {
final StringBuffer strbuf = new StringBuffer();
strbuf.append("longitude=" + mLongitude);
strbuf.append("&latitude=" + mLatitude);
strbuf.append("&time=" + mStartTime);
strbuf.append("&meid=" + mIMEIStr);
strbuf.append("&tel=" + mMobile);
strbuf.append("&iccid=" + mICCIDStr);
strbuf.append("&imsi=" + mIMSIStr);
strbuf.append("&model=" + mModel);
strbuf.append("&cfgv=" + ret_cfgv);
strbuf.append("&gcs=" + "GCJ02");
Log.d(TAG, "REQUEST: URL: " + mPath + ", BODY:" + strbuf);
URL url = null;
HttpURLConnection httpURLConnection = null;
OutputStream os = null;
try {
url = new URL(mPath);
} catch (Exception e) {
e.printStackTrace();
}
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();
DevicePreferences.getInstance(mContext).putString("bird_ret_cfgv", ret_cfgv);
Log.d(TAG, "server response: " + sb);
}else if (ret_code == 0){
Log.d(TAG, "upload success");
}else{
Log.d(TAG, "upload failed");
}
JSONObject jsonObject = new JSONObject(sb.toString());
ret_code = jsonObject.getInt("ret_code");
if (ret_code == 1){
ret_cfgv = jsonObject.getString("cfgv");
mPath = jsonObject.getString("url");
Log.d(TAG, "new path: " + mPath);
setAlarm();
if (!mPath.equals("0")){
DevicePreferences.getInstance(mContext).putString("bird_location_upload_url", mPath);
}
int timeSpanMinutes = jsonObject.getInt("time_span");
if (timeSpanMinutes == 999999){
DevicePreferences.getInstance(mContext).putLong("bird_location_upload_interval", 0);
}else if (timeSpanMinutes == 0){
Log.d(TAG, "time span not change");
}else{
DevicePreferences.getInstance(mContext).putLong("bird_location_upload_interval", timeSpanMinutes);
}
// close gps & network.
Intent intent = new Intent("com.bird.close_gps_switch");
intent.addFlags(0x01000000);
getApplicationContext().sendBroadcast(intent);
DevicePreferences.getInstance(mContext).putString("bird_ret_cfgv", ret_cfgv);
}
}else if (ret_code == 0){
Log.d(TAG, "upload success");
}else{
Log.d(TAG, "upload failed");
}
@Override
public void onFailed(Exception e) {
Log.d(TAG, "onFailed Exception:" + e.toString());
setAlarm();
/*mErrorCount++;
if (mErrorCount > 5){
} else if (responseCode != 200) {
Log.d(TAG, "request failed");
}
setAlarm();
// close gps & network.
Intent intent = new Intent("com.bird.close_gps_switch");
intent.addFlags(0x01000000);
getApplicationContext().sendBroadcast(intent);
} catch (Exception e) {
Log.d(TAG, "Exception:" + e.toString());
setAlarm();
}else{
long temp = DevicePreferences.getInstance(mContext).getLong("bird_location_upload_interval", DEFAULTINTERVAL);
mErrorHandler.sendEmptyMessageDelayed(0, temp);
}*/
mErrorCount++;
if (mErrorCount > 5){
setAlarm();
}else{
long temp = DevicePreferences.getInstance(mContext).getLong("bird_location_upload_interval", DEFAULTINTERVAL);
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) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!