У меня есть логическое имя, которое создано от public static boolean checkIfOnline()
. Но я хочу сделать систему проверки (от значения) с public void detectonline()
. Но когда я хочу позвонить по телефону my protected void onCreate
. У меня есть эта ошибка: Unhandled exceptions: org.json.JSONException, java.io.IOException
на моем detectonline();
(в onCreate).If Statement with JSON Data from boolean
public class notification extends AppCompatActivity {
Boolean onair;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification);
detectonline();
}
public void detectonline() throws JSONException, IOException {
checkIfOnline();
final TextView t=(TextView)findViewById(R.id.textView); // Hors ligne
final TextView t2=(TextView)findViewById(R.id.textView2); // En ligne
if (onair == false) {
t.setVisibility(View.VISIBLE);
t2.setVisibility(View.INVISIBLE);
} else {
t.setVisibility(View.INVISIBLE);
t2.setVisibility(View.VISIBLE);
}
}
public static boolean checkIfOnline() throws JSONException, IOException {
String channerUrl = "https://api.dailymotion.com/video/x3p6d9r?fields=onair";
String jsonText = readFromUrl(channerUrl);// reads text from URL
JSONObject json = new JSONObject(jsonText); // You create a json object from your string jsonText
if(json.has("onair")) { // just a simple test to check the node that you need existe
boolean onair = json.getBoolean("onair"); // In the url that you gave onair value is boolean type
return onair;
}
return false;
}
private static String readFromUrl(String url) throws IOException {
URL page = new URL(url);
StringBuilder sb = new StringBuilder();
Scanner scanner = null;
try{
//scanner = new Scanner(page.openStream(), StandardCharsets.UTF_8.name()); Encodage qui merde avant l'API 19 d'android
scanner = new Scanner(page.openStream());
while (scanner.hasNextLine()){
sb.append(scanner.nextLine());
}
} finally {
if (scanner!=null)
scanner.close();
}
return sb.toString();
}
}
Как проверить, является ли логическое значение истинным или ложным, и как выполняется действие с результатом?
Когда вы бросаете исключение в Java, вы ДОЛЖНЫ его уловить. Это ваша обязанность сделать это, если вы не платите захват Exception, чтобы сделать это за вас! – Eenvincible