同步请求接口

This commit is contained in:
Jerry Yan 2020-03-14 17:27:42 +08:00
parent f595efe355
commit 53c8e212aa

View File

@ -33,6 +33,7 @@ import okhttp3.Response;
public class UpgradeModule extends ReactContextBaseJavaModule {
private final ReactApplicationContext reactContext;
private String DOWNLOAD_URL = "http://luntan.qgmzbxs.com/source/tsgzCircles.apk";
private String HOST = "http://luntan.qgmzbxs.com/";
private String TITLE = "更新";
private String CONTENT = "大家好,我是勤劳的催更新菌\n点击更新让我们一起成为一天更新八次的暴躁老哥吧";
@ -94,14 +95,18 @@ public class UpgradeModule extends ReactContextBaseJavaModule {
}
private void _startDownload(){
OkHttpClient okHttpClient = new OkHttpClient();
final Request request = new Request.Builder()
.url(DOWNLOAD_URL)
.method("GET", null)
.build();
Call call = okHttpClient.newCall(request);
}
@ReactMethod
public void checkUpgrade(){
final UpgradeModule that = this;
OkHttpClient okHttpClient = new OkHttpClient();
String upgradeUrl = "https://www.tsgzvore.com/versionCheck";
String upgradeUrl = HOST.concat("versionCheck");
PackageManager manager = reactContext.getPackageManager();
try {
String packageName = reactContext.getPackageName();
@ -121,50 +126,25 @@ public class UpgradeModule extends ReactContextBaseJavaModule {
.method("GET", null)
.build();
Call call = okHttpClient.newCall(request);
final Handler handler = new ShowUpgradeAlertHandler(that);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e("VERSION CHECK", call.toString(), e);
}
@Override
public void onResponse(Call call, Response response) {
if(response.isSuccessful()){
if(response.body() == null) return;
try {
String jsonText = response.body().string();
Log.d("VERSION CHECK", jsonText);
JSONObject json = new JSONObject(jsonText);
int status = json.getInt("status");
if(status > 0){
CONTENT = json.getString("message");
// String version = json.getString("version");
JSONObject jsonData = json.getJSONObject("data");
DOWNLOAD_URL = jsonData.getString("url");
handler.sendEmptyMessage(0);
}
} catch (IOException e) {
Log.e("REQUEST FAILED", "AAA", e);
} catch (JSONException e) {
Log.e("JSON FAILED", "BBB", e);
}
try {
Response response = call.execute();
if(response.isSuccessful()){
if(response.body() == null) return;
String jsonText = response.body().string();
Log.d("VERSION CHECK", jsonText);
JSONObject json = new JSONObject(jsonText);
int status = json.getInt("status");
if(status > 0){
CONTENT = json.getString("message");
JSONObject jsonData = json.getJSONObject("data");
DOWNLOAD_URL = jsonData.getString("url");
this._alertDialog();
}
}
});
}
}
final class ShowUpgradeAlertHandler extends Handler
{
private UpgradeModule module;
ShowUpgradeAlertHandler(UpgradeModule module)
{
this.module = module;
}
@Override
public void handleMessage(Message message){
module._alertDialog();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
Log.e("JSON FAILED", "BBB", e);
}
}
}