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,67 +256,49 @@ public class BirdService extends Service { ...@@ -234,67 +256,49 @@ 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 run() { public void handleMessage(Message msg) {
final StringBuffer strbuf = new StringBuffer(); postLocationInfo();
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();
} }
};
private void postLocationInfo() {
JSONObject jsonBody = new JSONObject();
try { try {
httpURLConnection = (HttpURLConnection) url.openConnection(); jsonBody.put("longitude", mLongitude);
httpURLConnection.setRequestMethod("POST"); jsonBody.put("latitude", mLatitude);
httpURLConnection.setReadTimeout(5000); jsonBody.put("time", mStartTime);
httpURLConnection.setDoOutput(true); jsonBody.put("meid", mIMEIStr);
httpURLConnection.setRequestProperty("Charset", "UTF-8"); jsonBody.put("tel", mMobile);
httpURLConnection.setRequestProperty("Content-Length", strbuf.toString().length() + ""); jsonBody.put("iccid", mICCIDStr);
os = httpURLConnection.getOutputStream(); jsonBody.put("imsi", mIMSIStr);
os.write(strbuf.toString().getBytes()); jsonBody.put("model", mModel);
os.flush(); jsonBody.put("cfgv", ret_cfgv);
int responseCode = httpURLConnection.getResponseCode(); jsonBody.put("gcs", "GCJ02");
Log.d(TAG, "responseCode--" + responseCode); } catch (JSONException x) {
if (responseCode == HttpURLConnection.HTTP_OK) { x.printStackTrace();
InputStreamReader is = new InputStreamReader(httpURLConnection.getInputStream()); }
StringBuffer sb = new StringBuffer(); Log.d(TAG, "url: " + mPath + ", jsonBody: " + jsonBody.toString());
int i = 0;
while ((i = is.read()) != -1) { IotApi.post(mPath, jsonBody.toString() , new IotNetApiCallback() {
sb.append((char) i); @Override
} public void onSuccess(String response) {
is.close(); Log.d(TAG, "server response: " + response);
JsonObject resp = new JsonParser().parse(response).getAsJsonObject();
Log.d(TAG, "server response: " + sb);
ret_code = resp.get("ret_code").getAsInt();
JSONObject jsonObject = new JSONObject(sb.toString());
ret_code = jsonObject.getInt("ret_code");
if (ret_code == 1){ if (ret_code == 1){
ret_cfgv = jsonObject.getString("cfgv"); ret_cfgv = resp.get("cfgv").getAsString();
mPath = jsonObject.getString("url"); mPath = resp.get("url").getAsString();
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 = jsonObject.getInt("time_span"); int timeSpanMinutes = resp.get("time_span").getAsInt();
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){
...@@ -311,10 +315,6 @@ public class BirdService extends Service { ...@@ -311,10 +315,6 @@ 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.
...@@ -322,7 +322,10 @@ public class BirdService extends Service { ...@@ -322,7 +322,10 @@ public class BirdService extends Service {
intent.addFlags(0x01000000); intent.addFlags(0x01000000);
getApplicationContext().sendBroadcast(intent); getApplicationContext().sendBroadcast(intent);
} catch (Exception e) { }
@Override
public void onFailed(Exception e) {
Log.d(TAG, "Exception:" + e.toString()); Log.d(TAG, "Exception:" + e.toString());
mErrorCount++; mErrorCount++;
if (mErrorCount > 5){ if (mErrorCount > 5){
...@@ -333,21 +336,11 @@ public class BirdService extends Service { ...@@ -333,21 +336,11 @@ public class BirdService extends Service {
intent.addFlags(0x01000000); intent.addFlags(0x01000000);
getApplicationContext().sendBroadcast(intent); getApplicationContext().sendBroadcast(intent);
}else{ }else{
sendMsg(); 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) { 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!