私には、public static boolean checkIfOnline()
から作成されるブール値があります。しかし、私はpublic void detectonline()
で(値からの)チェックシステムを作りたいと思っています。しかし、私はmy protected void onCreate
に電話したいと思っています。このエラーはUnhandled exceptions: org.json.JSONException, java.io.IOException
on detectonline();
(onCreate内)です。If Statement with JSONブール値のデータ
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で例外を投げるときは、どこかで例外をキャッチしなければなりません。例外的なキャッチャーに支払う場合を除いて、それを行うのはあなたの責任です! – Eenvincible