gpt4 book ai didi

java - AsyncTask 在后台方法 try/catch 中执行,两者都被执行

转载 作者:行者123 更新时间:2023-11-30 05:20:15 29 4
gpt4 key购买 nike

我正在使用async任务从url获取Json数据。正如你所看到的,在日志 cat 中,我得到了 url 输出,但 catch block 仍然被执行,我无法找出错误

代码:

public class MainActivity extends AppCompatActivity {
Scanner scanner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
weatherTask task = new weatherTask();
task.execute();
}
private class weatherTask extends AsyncTask<String,Void,String> {
@Override
protected String doInBackground(String... strings) {
try{
URL url = new URL("http://api.openweathermap.org/data/2.5/weather?lat=28.732754&lon=77.175051&appid=e6a99aef963a3e835da961d3cf9aeeb6");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = urlConnection.getInputStream();
scanner = new Scanner(in);
scanner.useDelimiter("\\A");
boolean has_input = scanner.hasNext();
if(has_input){
Log.d("url output",scanner.next());
String result = scanner.next().toString();
return result;
}
}
catch (Exception e){
// errorText.setVisibility(View.VISIBLE);
Log.i("inside","do in back");
// relativeLayout.setVisibility( View.GONE);
e.printStackTrace();
}
return null;
}
}
}



日志猫

2020-01-12 20:54:51.814 17593-17663/? D/url output: {"coord":{"lon":77.18,"lat":28.73},"weather":[{"id":711,"main":"Smoke","description":"smoke","icon":"50n"}],"base":"stations","main":{"temp":289.2,"feels_like":288.97,"temp_min":288.15,"temp_max":289.82,"pressure":1014,"humidity":67},"visibility":2200,"wind":{"speed":0.37,"deg":12},"clouds":{"all":20},"dt":1578842444,"sys":{"type":1,"id":9165,"country":"IN","sunrise":1578793526,"sunset":1578831159},"timezone":19800,"id":1269125,"name":"Jawaharnagar","cod":200}
2020-01-12 20:54:51.814 17593-17663/? I/inside: do in back
2020-01-12 20:54:51.814 17593-17663/? W/System.err: java.util.NoSuchElementException
2020-01-12 20:54:51.815 17593-17663/? W/System.err: at java.util.Scanner.throwFor(Scanner.java:874)
2020-01-12 20:54:51.815 17593-17663/? W/System.err: at java.util.Scanner.next(Scanner.java:1388)
2020-01-12 20:54:51.815 17593-17663/? W/System.err: at com.tarandeepsingh.test.MainActivity$weatherTask.doInBackground(MainActivity.java:91)
2020-01-12 20:54:51.815 17593-17663/? W/System.err: at com.tarandeepsingh.test.MainActivity$weatherTask.doInBackground(MainActivity.java:24)
2020-01-12 20:54:51.815 17593-17663/? W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:333)
2020-01-12 20:54:51.815 17593-17663/? W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:266)
2020-01-12 20:54:51.815 17593-17663/? W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
2020-01-12 20:54:51.815 17593-17663/? W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
2020-01-12 20:54:51.815 17593-17663/? W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
2020-01-12 20:54:51.815 17593-17663/? W/System.err: at java.lang.Thread.run(Thread.java:764)

谢谢

最佳答案

NoSuchElementException 表示没有更多可用 token 。请尝试以下

boolean has_input = scanner.hasNext();

if(has_input) {
String result = scanner.next();

Log.d("url output", result);

return result;
}

关于java - AsyncTask 在后台方法 try/catch 中执行,两者都被执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59705327/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com