同步请求接口

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