2016-04-16 1 views
0

私は読み取り出力フォーム要求に問題があります。 AsyncTask LIFX Bulb resposne

StackTrace

public JSONArray listLights() 
{ 
    try 
    { 
     URL adres = new URL("https://api.lifx.com/v1/lights/all"); 
     HttpURLConnection polaczenie = (HttpURLConnection) adres.openConnection(); 
     polaczenie.setRequestProperty("Authorization", "Bearer " + apiKey); 
     polaczenie.setRequestMethod("GET"); 

     BufferedReader wejscie = new BufferedReader(new InputStreamReader((polaczenie.getInputStream()))); 
     StringBuilder odpowiedz = new StringBuilder(); 

     String json; 
     while ((json = wejscie.readLine()) != null) 
      odpowiedz.append(json); 
     wejscie.close(); 

     return new JSONArray(odpowiedz.toString()); 
    } 
    catch (Exception wyjatek) 
    { 
     wyjatek.printStackTrace(); 
    } 
    return new JSONArray(); 
} 

は私もAndroidManifestインターネットアクセスに加えます。

ようこそコメントを残してください。 :P


EDIT:

私はインターネットをGoogleや部分的な解決策を見つけました。 AsyncTaskを追加しましたが、今は '429'のレスポンスコードを受け取ります。

public class JSONTask extends AsyncTask<String, String, String> 
{ 
    String apiKey = "blah_blah_blah"; 
    String txtresult; 

    @Override 
    protected String doInBackground(String... params) { 
     HttpsURLConnection connection = null; 
     BufferedReader reader = null; 

     try 
     { 
      URL adres = new URL(params[0]); 
      HttpsURLConnection polaczenie = (HttpsURLConnection) adres.openConnection(); 
      polaczenie.setRequestProperty("Authorization", "Bearer " + apiKey); 
      polaczenie.setRequestMethod("GET"); 

      System.out.println(polaczenie.getResponseCode()); 

      InputStream stream = polaczenie.getInputStream(); 
      reader = new BufferedReader(new InputStreamReader(stream)); 
      StringBuffer buffer = new StringBuffer(); 

      String line = ""; 
      while ((line = reader.readLine()) != null) 
      { 
       buffer.append(line); 
      } 
      return buffer.toString(); 
     } 
     catch (MalformedURLException e) 
     { 
      e.printStackTrace(); 
     } 
     catch (IOException e) 
     { 
      e.printStackTrace(); 
     } 
     finally { 
      if (connection != null) 
       connection.disconnect(); 
      try 
      { 
       if (reader != null) 
        reader.close(); 
      } 
      catch (IOException e) 
      { 
       e.printStackTrace(); 
      } 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(String s) 
    { 
     super.onPostExecute(s); 
     widok.setText(s); 
    } 
} 

私の現在のStackTrace


EDIT2:

新しい日、新しい驚き。私は、10回の試行ごとに1回または2回Bulbと接続していることを理解しています。何か案は?

答えて

0

HTTPステータスコード429は、一定の時間内に多すぎるリクエストを意味します。どのくらい多くのリクエストをしていますか?

+0

私は知っている、私はボタンをクリックするごとに1つのreqestを送信しています。 public void sendGetRequest(ビューを表示){ 新しいJSONTask()。execute( "https://api.lifx.com/v1/lights/all"); } – nextion24

0

android.os.NetworkOnMainThreadExceptionつまり、UIthread以外の脅威からhtttpリクエストを行う必要があります。なぜ非同期タスクを使用していますか?

編集:また、郵便配達員から電話をかけてみることもできますが、問題が表示されることがあります。

0

最終的にはすべてが機能しています。問題は電球やLifx Cloudの側面にあった。