2016-08-30 6 views
0

私には、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(); 
    } 
} 

ブール値が真または偽であるかどうか、どうしたら結果を確認できますか?

+0

Javaで例外を投げるときは、どこかで例外をキャッチしなければなりません。例外的なキャッチャーに支払う場合を除いて、それを行うのはあなたの責任です! – Eenvincible

答えて

-1

まず、checkIfOnline()関数はbooleanを返しますが、空白として使用しています。つまり、戻り値をどこにも割り当てていません。あなたはブール値で宣言しているようにcheckIfOnline()関数の中で、この

public static void checkIfOnline() 

第二に、この

public static boolean checkIfOnline() 

を変更するには、ローカル変数ONAIRにブールのに値を代入しています機能自体はこっち:

boolean onair = json.getBoolean("onair"); 

そうちょうどのように変更します。

onair = json.getBoolean("onair"); 

最後に、onair変数をprivate/public変数として宣言し、関数がそれを変更できるようにします(また、上記のコードで記述されているようにブール値ではなくブール値を使用します)。だから、これは問題を解決する可能性がある。この

private static boolean onair; 

にこの

Boolean onair; 

を変更。そうでない場合、私はあなたに良い答えを与えますが、まずこれを試してください。

+0

ありがとう、あなたが助けてくれますが、 'protected void onCreate'で、' detectonline(); 'を呼びたい場合、'ハンドルされていない例外:org.json.JSONException、java.io.IOException'と同じ問題があります。 – PotatoesPower

0
  1. グローバル変数onairは決して初期化されません。

    onair = checkIfOnline();

  2. このメソッドを呼び出すたびメソッドdetectionline()

それらを引くことにより、または再投げそれらのいずれかによって、あなたは例外のこの種に対処する必要がありますので、
try{ 
     detectionline(); 
    }catch(JSONException | IOException e){ 
     e.printstacktrace(); 
    } 
JSONExceptionIOExceptionをスローします