【问题标题】:AsyncTask LIFX Bulb responseAsyncTask LIFX 灯泡响应
【发布时间】:2016-08-08 13:49:30
【问题描述】:

我遇到了读取输出表单请求的问题。

    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

我也加入了 AndroidManifest 上网。

欢迎留下任何cmets。 :P


编辑:

我在网上搜索并找到了部分解决方案。添加了 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 次尝试就与 Bulb 建立一次/两次联系。有什么想法吗?

【问题讨论】:

    标签: java android json android-asynctask httpsurlconnection


    【解决方案1】:

    HTTP 状态码 429 表示在给定时间内请求过多。那么你到底做了多少请求呢?

    【讨论】:

    • 我知道,每次单击按钮我只会发送一个请求。 public void sendGetRequest(View view) { new JSONTask().execute("api.lifx.com/v1/lights/all"); }
    【解决方案2】:

    android.os.NetworkOnMainThreadException 这意味着,您必须从 UIthread 之外的另一个威胁发出 HTTP 请求。你为什么使用异步任务?

    编辑:您也可以尝试从邮递员拨打电话,也许您会看到问题。

    【讨论】:

      【解决方案3】:

      最后,一切正常。问题出在灯泡或 Lifx Cloud 一侧。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-26
        • 2013-07-02
        • 1970-01-01
        • 2017-04-22
        相关资源
        最近更新 更多