Commit ed730052 by cuiliang.shi

okhttp 网络优化

1 parent 05da71ce
...@@ -23,6 +23,10 @@ android { ...@@ -23,6 +23,10 @@ android {
outputFileName = "LocationServer.apk" outputFileName = "LocationServer.apk"
} }
} }
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
} }
dependencies { dependencies {
...@@ -34,6 +38,8 @@ dependencies { ...@@ -34,6 +38,8 @@ dependencies {
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.amap.api:location:4.5.1' implementation 'com.amap.api:location:4.5.1'
implementation 'com.amap.api:map2d:5.2.0' implementation 'com.amap.api:map2d:5.2.0'
implementation("com.squareup.okhttp3:okhttp:3.14.1")
implementation 'com.google.code.gson:gson:2.8.2'
} }
...@@ -26,15 +26,21 @@ import android.support.annotation.RequiresApi; ...@@ -26,15 +26,21 @@ import android.support.annotation.RequiresApi;
import android.telephony.SubscriptionInfo; import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager; import android.telephony.SubscriptionManager;
import android.util.Log; import android.util.Log;
import android.view.View;
import android.widget.Toast; import android.widget.Toast;
import com.agenew.location_server.application.api.IotApi;
import com.agenew.location_server.application.api.IotNetApiCallback;
import com.amap.api.location.AMapLocation; import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient; import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption; import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener; import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps2d.AMapUtils; import com.amap.api.maps2d.AMapUtils;
import com.amap.api.maps2d.model.LatLng; import com.amap.api.maps2d.model.LatLng;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.io.IOException; import java.io.IOException;
...@@ -194,7 +200,7 @@ public class BirdService extends Service { ...@@ -194,7 +200,7 @@ public class BirdService extends Service {
} }
mLocationCount = 0; mLocationCount = 0;
getPhoneInfo(); getPhoneInfo();
sendMsg(); postLocationInfo();
} }
} else { } else {
...@@ -202,13 +208,29 @@ public class BirdService extends Service { ...@@ -202,13 +208,29 @@ public class BirdService extends Service {
Log.e(TAG, "location Error, ErrCode:" Log.e(TAG, "location Error, ErrCode:"
+ amapLocation.getErrorCode() + ", errInfo:" + amapLocation.getErrorCode() + ", errInfo:"
+ amapLocation.getErrorInfo()); + amapLocation.getErrorInfo());
handlerGpsError();
} }
} else { } else {
Log.d(TAG, "location Error"); Log.d(TAG, "location Error");
handlerGpsError();
} }
} }
}; };
private void handlerGpsError() {
mErrorCount++;
if (mErrorCount > 6){
setAlarm();
// close gps & network.
Intent intent = new Intent("com.bird.close_gps_switch");
intent.addFlags(0x01000000);
getApplicationContext().sendBroadcast(intent);
}
}
public void setAlarm(){ public void setAlarm(){
Log.d(TAG, "setAlarm"); Log.d(TAG, "setAlarm");
ret_cfgv = DevicePreferences.getInstance(getApplicationContext()).getString("bird_ret_cfgv", "2019031512"); ret_cfgv = DevicePreferences.getInstance(getApplicationContext()).getString("bird_ret_cfgv", "2019031512");
...@@ -234,120 +256,91 @@ public class BirdService extends Service { ...@@ -234,120 +256,91 @@ public class BirdService extends Service {
} }
private int mErrorCount = 0; private int mErrorCount = 0;
public void sendMsg() { @SuppressLint("HandlerLeak")
Log.d(TAG, "sendMsg"); private Handler mErrorHandler = new Handler(){
new Thread(new Runnable() { @Override
@Override public void handleMessage(Message msg) {
public void run() { postLocationInfo();
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(5000);
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()); private void postLocationInfo() {
ret_code = jsonObject.getInt("ret_code"); JSONObject jsonBody = new JSONObject();
if (ret_code == 1){ try {
ret_cfgv = jsonObject.getString("cfgv"); jsonBody.put("longitude", mLongitude);
mPath = jsonObject.getString("url"); jsonBody.put("latitude", mLatitude);
Log.d(TAG, "new path: " + mPath); 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());
if (!mPath.equals("0")){ IotApi.post(mPath, jsonBody.toString() , new IotNetApiCallback() {
DevicePreferences.getInstance(mContext).putString("bird_location_upload_url", mPath); @Override
} public void onSuccess(String response) {
int timeSpanMinutes = jsonObject.getInt("time_span"); Log.d(TAG, "server response: " + response);
if (timeSpanMinutes == 999999){ JsonObject resp = new JsonParser().parse(response).getAsJsonObject();
DevicePreferences.getInstance(mContext).putLong("bird_location_upload_interval", 0);
}else if (timeSpanMinutes == 0){ ret_code = resp.get("ret_code").getAsInt();
Log.d(TAG, "time span not change"); if (ret_code == 1){
}else{ ret_cfgv = resp.get("cfgv").getAsString();
DevicePreferences.getInstance(mContext).putLong("bird_location_upload_interval", timeSpanMinutes); 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);
}
DevicePreferences.getInstance(mContext).putString("bird_ret_cfgv", ret_cfgv); DevicePreferences.getInstance(mContext).putString("bird_ret_cfgv", ret_cfgv);
}else if (ret_code == 0){ }else if (ret_code == 0){
Log.d(TAG, "upload success"); Log.d(TAG, "upload success");
}else{ }else{
Log.d(TAG, "upload failed"); Log.d(TAG, "upload failed");
} }
} else if (responseCode != 200) { setAlarm();
Log.d(TAG, "request failed");
} // close gps & network.
Intent intent = new Intent("com.bird.close_gps_switch");
intent.addFlags(0x01000000);
getApplicationContext().sendBroadcast(intent);
}
@Override
public void onFailed(Exception e) {
Log.d(TAG, "Exception:" + e.toString());
mErrorCount++;
if (mErrorCount > 5){
setAlarm(); setAlarm();
// close gps & network. // close gps & network.
Intent intent = new Intent("com.bird.close_gps_switch"); Intent intent = new Intent("com.bird.close_gps_switch");
intent.addFlags(0x01000000); intent.addFlags(0x01000000);
getApplicationContext().sendBroadcast(intent); getApplicationContext().sendBroadcast(intent);
}else{
} catch (Exception e) { long temp = DevicePreferences.getInstance(mContext).getLong("bird_location_upload_interval", DEFAULTINTERVAL);
Log.d(TAG, "Exception:" + e.toString()); mErrorHandler.sendEmptyMessageDelayed(0, temp);
mErrorCount++;
if (mErrorCount > 5){
setAlarm();
// close gps & network.
Intent intent = new Intent("com.bird.close_gps_switch");
intent.addFlags(0x01000000);
getApplicationContext().sendBroadcast(intent);
}else{
sendMsg();
}
} 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) {
...@@ -388,6 +381,9 @@ public class BirdService extends Service { ...@@ -388,6 +381,9 @@ public class BirdService extends Service {
// String temp_model = Build.MODEL; // String temp_model = Build.MODEL;
String temp_model = PhoneUtils.getProperty("ro.qiku.custom.model", "undefined"); String temp_model = PhoneUtils.getProperty("ro.qiku.custom.model", "undefined");
if (temp_model.equals("undefined")){
temp_model = Build.MODEL;
}
mModel = temp_model.trim().replaceAll(" ", ""); mModel = temp_model.trim().replaceAll(" ", "");
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!