Java 异步任务LIFX灯泡响应
我在读取输出表单请求时遇到问题Java 异步任务LIFX灯泡响应,java,android,json,android-asynctask,httpsurlconnection,Java,Android,Json,Android Asynctask,Httpsurlconnection,我在读取输出表单请求时遇到问题 public JSONArray listLights() { try { URL adres = new URL("https://api.lifx.com/v1/lights/all"); HttpURLConnection polaczenie = (HttpURLConnection) adres.openConnection(); polaczenie.setRequestPrope
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 Internet访问
欢迎发表意见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);
}
}
公共类JSONTask扩展异步任务
{
字符串apiKey=“诸如此类”;
字符串txtresult;
@凌驾
受保护的字符串doInBackground(字符串…参数){
HttpsURLConnection连接=null;
BufferedReader reader=null;
尝试
{
URL adres=新URL(参数[0]);
HttpsURLConnection polaczenie=(HttpsURLConnection)adres.openConnection();
polaczenie.setRequestProperty(“授权”、“持有人”+apiKey);
setRequestMethod(“GET”);
System.out.println(polaczenie.getResponseCode());
InputStream=polaczenie.getInputStream();
reader=新的BufferedReader(新的InputStreamReader(流));
StringBuffer=新的StringBuffer();
字符串行=”;
而((line=reader.readLine())!=null)
{
buffer.append(行);
}
返回buffer.toString();
}
捕获(格式错误)
{
e、 printStackTrace();
}
捕获(IOE异常)
{
e、 printStackTrace();
}
最后{
if(连接!=null)
连接断开();
尝试
{
if(读卡器!=null)
reader.close();
}
捕获(IOE异常)
{
e、 printStackTrace();
}
}
返回null;
}
@凌驾
受保护的void onPostExecute(字符串s)
{
super.onPostExecute(s);
widok.setText(s);
}
}
我的电流
编辑2:
新的一天,新的惊喜。我发现我每10次尝试连接灯泡一次/两次。有什么想法吗?HTTP状态代码429表示在给定的时间内请求过多。那么您到底在执行多少个请求呢?android.os.NetworkOnMainThreadException这意味着您必须从UIthread以外的其他威胁发出htttp请求。为什么要使用异步任务
编辑:你也可以试着从邮递员那里打个电话,也许你会发现问题所在。最后,一切正常。问题出在灯泡或Lifx云的一侧 我知道,我每次点击一个按钮只发送一个请求。public void sendGetRequest(视图){new JSONTask().execute(“);}