还是用异步吧

This commit is contained in:
2020-03-14 17:32:23 +08:00
parent 63c0c22419
commit 8f6ba4a99a

View File

@@ -68,7 +68,7 @@ public class UpgradeModule extends ReactContextBaseJavaModule {
} }
} }
private void _alertDialog(){ void _alertDialog(){
final UpgradeModule that = this; final UpgradeModule that = this;
AlertDialog dialog = new AlertDialog.Builder(reactContext.getCurrentActivity()) AlertDialog dialog = new AlertDialog.Builder(reactContext.getCurrentActivity())
.setTitle(TITLE) .setTitle(TITLE)
@@ -124,25 +124,50 @@ public class UpgradeModule extends ReactContextBaseJavaModule {
.method("GET", null) .method("GET", null)
.build(); .build();
Call call = okHttpClient.newCall(request); Call call = okHttpClient.newCall(request);
try { final Handler handler = new ShowUpgradeAlertHandler(this);
Response response = call.execute(); 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.isSuccessful()){
if(response.body() == null) return; if(response.body() == null) return;
try {
String jsonText = response.body().string(); String jsonText = response.body().string();
Log.d("VERSION CHECK", jsonText); Log.d("VERSION CHECK", jsonText);
JSONObject json = new JSONObject(jsonText); JSONObject json = new JSONObject(jsonText);
int status = json.getInt("status"); int status = json.getInt("status");
if(status > 0){ if(status > 0){
CONTENT = json.getString("message"); CONTENT = json.getString("message");
// String version = json.getString("version");
JSONObject jsonData = json.getJSONObject("data"); JSONObject jsonData = json.getJSONObject("data");
DOWNLOAD_URL = jsonData.getString("url"); DOWNLOAD_URL = jsonData.getString("url");
this._alertDialog(); handler.sendEmptyMessage(0);
}
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); Log.e("REQUEST FAILED", "AAA", e);
} catch (JSONException e) { } catch (JSONException e) {
Log.e("JSON FAILED", "BBB", 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();
}
} }