Commit ed730052 by cuiliang.shi

okhttp 网络优化

1 parent 05da71ce
......@@ -23,6 +23,10 @@ android {
outputFileName = "LocationServer.apk"
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
......@@ -34,6 +38,8 @@ dependencies {
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.amap.api:location:4.5.1'
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;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.util.Log;
import android.view.View;
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.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps2d.AMapUtils;
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 java.io.IOException;
......@@ -194,7 +200,7 @@ public class BirdService extends Service {
}
mLocationCount = 0;
getPhoneInfo();
sendMsg();
postLocationInfo();
}
} else {
......@@ -202,13 +208,29 @@ public class BirdService extends Service {
Log.e(TAG, "location Error, ErrCode:"
+ amapLocation.getErrorCode() + ", errInfo:"
+ amapLocation.getErrorInfo());
handlerGpsError();
}
} else {
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(){
Log.d(TAG, "setAlarm");
ret_cfgv = DevicePreferences.getInstance(getApplicationContext()).getString("bird_ret_cfgv", "2019031512");
......@@ -234,67 +256,49 @@ public class BirdService extends Service {
}
private int mErrorCount = 0;
public void sendMsg() {
Log.d(TAG, "sendMsg");
new Thread(new Runnable() {
@SuppressLint("HandlerLeak")
private Handler mErrorHandler = new Handler(){
@Override
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();
public void handleMessage(Message msg) {
postLocationInfo();
}
};
private void postLocationInfo() {
JSONObject jsonBody = new JSONObject();
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());
ret_code = jsonObject.getInt("ret_code");
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());
IotApi.post(mPath, jsonBody.toString() , new IotNetApiCallback() {
@Override
public void onSuccess(String response) {
Log.d(TAG, "server response: " + response);
JsonObject resp = new JsonParser().parse(response).getAsJsonObject();
ret_code = resp.get("ret_code").getAsInt();
if (ret_code == 1){
ret_cfgv = jsonObject.getString("cfgv");
mPath = jsonObject.getString("url");
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 = jsonObject.getInt("time_span");
int timeSpanMinutes = resp.get("time_span").getAsInt();
if (timeSpanMinutes == 999999){
DevicePreferences.getInstance(mContext).putLong("bird_location_upload_interval", 0);
}else if (timeSpanMinutes == 0){
......@@ -311,10 +315,6 @@ public class BirdService extends Service {
Log.d(TAG, "upload failed");
}
} else if (responseCode != 200) {
Log.d(TAG, "request failed");
}
setAlarm();
// close gps & network.
......@@ -322,7 +322,10 @@ public class BirdService extends Service {
intent.addFlags(0x01000000);
getApplicationContext().sendBroadcast(intent);
} catch (Exception e) {
}
@Override
public void onFailed(Exception e) {
Log.d(TAG, "Exception:" + e.toString());
mErrorCount++;
if (mErrorCount > 5){
......@@ -333,21 +336,11 @@ public class BirdService extends Service {
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();
}
}
long temp = DevicePreferences.getInstance(mContext).getLong("bird_location_upload_interval", DEFAULTINTERVAL);
mErrorHandler.sendEmptyMessageDelayed(0, temp);
}
}
}).start();
});
}
private void initLocation(Context context) {
......@@ -388,6 +381,9 @@ public class BirdService extends Service {
// String temp_model = Build.MODEL;
String temp_model = PhoneUtils.getProperty("ro.qiku.custom.model", "undefined");
if (temp_model.equals("undefined")){
temp_model = Build.MODEL;
}
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!