0

I have got problem with read output form request.

    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();
}

StackTrace

I added to AndroidManifest Internet access too.

Welcome to leave any comments. :P


EDIT:

I google internet and found partial solution. Added AsyncTask, but now I'm receiving '429' response code.

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);
    }
}

My current StackTrace


EDIT2:

New day, new surprise. I figure out that I'm making connection with Bulb once/twice on every 10 attempts. Any ideas?

Cœur
  • 37,241
  • 25
  • 195
  • 267
nextion24
  • 51
  • 5

3 Answers3

0

HTTP Status code 429 means too many requests in a given an amount of time. So how many requests exactly are you doing?

  • I know, I'm sending only one reqest per one button click. public void sendGetRequest(View view) { new JSONTask().execute("https://api.lifx.com/v1/lights/all"); } – nextion24 Apr 17 '16 at 12:09
0

android.os.NetworkOnMainThreadException it means, that You have to make a htttp request from another threat than UIthread. Why are you using async task ?

Edit: You can also try make a call from postman and maybe You will see the problem.

Piotr Badura
  • 1,574
  • 1
  • 13
  • 17
0

In the end, everything is working. Problem was on the side of bulb or Lifx Cloud.

nextion24
  • 51
  • 5