Java 跳过了104帧!应用程序可能在其主线程上做了太多工作

Java 跳过了104帧!应用程序可能在其主线程上做了太多工作,java,android,android-studio,Java,Android,Android Studio,我查看了StackOverflow的答案,但没找到多少。所以,我这样做是为了练习,就像HelloWorld使用JSON一样,我从OpenWeatherAPI获得JSON响应。 我在EditText中写下城市的名称,然后按下按钮进行搜索,并在日志中显示JSON字符串 public class MainActivity extends AppCompatActivity { EditText city; public void getData(View view){ String resul

我查看了StackOverflow的答案,但没找到多少。所以,我这样做是为了练习,就像HelloWorld使用JSON一样,我从OpenWeatherAPI获得JSON响应。 我在EditText中写下城市的名称,然后按下按钮进行搜索,并在日志中显示JSON字符串

public class MainActivity extends AppCompatActivity {
EditText city;
public void getData(View view){
    String result;
    String cityName = city.getText().toString();
    getWeather weather = new getWeather();
    try {
        result = weather.execute(cityName).get();
        System.out.println(result);
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }
}

public class getWeather extends AsyncTask<String, Void, String>{

    @Override
    protected String doInBackground(String... urls) {
        URL url;
        HttpURLConnection connection = null;
        String result = "";

        try {
            String finalString = urls[0];
            finalString = finalString.replace(" ", "%20");
            String fullString = "http://api.openweathermap.org/data/2.5/forecast?q=" + finalString + "&appid=a18dc34257af3b9ce5b2347bb187f0fd";
            url = new URL(fullString);
            connection = (HttpURLConnection) url.openConnection();
            InputStream in = connection.getInputStream();
            InputStreamReader reader = new InputStreamReader(in);
            int data = reader.read();
            while(data != -1){
                char current = (char) data;
                result += current;
                data = reader.read();
            }
            return result;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;

    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    city = (EditText) findViewById(R.id.editText);
}
}
public类MainActivity扩展了AppCompatActivity{
编辑文本城市;
公共void getData(视图){
字符串结果;
字符串cityName=city.getText().toString();
getWeather=新的getWeather();
试一试{
结果=weather.execute(cityName.get();
系统输出打印项次(结果);
}捕捉(中断异常e){
e、 printStackTrace();
}捕获(执行例外){
e、 printStackTrace();
}
}
公共类getWeather扩展异步任务{
@凌驾
受保护的字符串doInBackground(字符串…URL){
网址;
HttpURLConnection=null;
字符串结果=”;
试一试{
字符串finalString=URL[0];
finalString=finalString.replace(“,“%20”);
字符串fullString=”http://api.openweathermap.org/data/2.5/forecast?q=“+finalString+”&appid=a18dc34257af3b9ce5b2347bb187f0fd”;
url=新url(完整字符串);
connection=(HttpURLConnection)url.openConnection();
InputStream in=connection.getInputStream();
InputStreamReader reader=新的InputStreamReader(in);
int data=reader.read();
while(数据!=-1){
当前字符=(字符)数据;
结果+=电流;
data=reader.read();
}
返回结果;
}捕获(格式错误){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
返回null;
}
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
城市=(EditText)findViewById(R.id.EditText);
}
}
我该怎么做才能不收到那个信息

weather.execute(cityName.get()

执行
get()
时,您正在等待异步任务完成。因此,您正在Ui线程上运行所有繁重的操作

发件人:

如有必要,等待计算完成,然后检索其结果

删除
get()

weather.execute(cityName.get()

执行
get()
时,您正在等待异步任务完成。因此,您正在Ui线程上运行所有繁重的操作

发件人:

如有必要,等待计算完成,然后检索其结果


删除
get()。异步任务的要点是并行运行。如果您觉得需要调用get(),那么首先就没有必要使用AsyncTask。在UI线程上这样做是绝对不可能的,你让你的整个应用程序没有响应,可能会被看门狗定时器杀死。.get适合的次数非常有限。好的,但是如果我删除.get(),我会得到一个错误“不兼容类型”。。那么,我应该使用什么来代替.get(),还是有更好的方法来获取JSON数据呢?我应该使用onPostExecute()?@VedranKorponajić,是的,成功完成后台作业后应该执行的所有逻辑都应该在
onPostExecute()
中完成。此外,还应该添加-从不调用.get()。异步任务的要点是并行运行。如果您觉得需要调用get(),那么首先就没有必要使用AsyncTask。在UI线程上这样做是绝对不可能的,你让你的整个应用程序没有响应,可能会被看门狗定时器杀死。.get适合的次数非常有限。好的,但是如果我删除.get(),我会得到一个错误“不兼容类型”。。那么,我应该使用什么来代替.get(),还是有更好的方法来获取JSON数据?我应该使用onPostExecute()?@VedranKorponajić,是的,成功完成后台作业后应该执行的所有逻辑都应该在
onPostExecute()
中完成。