【问题标题】:Volley in android giving Message Execution time is too long errorandroid中的Volley给出消息执行时间太长错误
【发布时间】:2021-03-24 12:16:10
【问题描述】:

我是 android 新手,请帮我解决这个问题。

我正在通过 volley API 获取 JSON,但它显示主线程上的工作量过多。

一切正常,但在提出请求时没有任何反应。 我在代码文件下面提到了错误消息。 这是关于性能的事情,但我不知道如何解决它。请帮忙。

当我调试它时,我发现在此之后代码没有执行,

 JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, readyUrl, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        JSONArray arr;
                        try {
                           arr = response.getJSONArray("weather");
                           JSONObject weatherObj = arr.getJSONObject(0);
                           String weather = weatherObj.getString("main");
                           String icon = weatherObj.getString("icon");
                           String iconUrl = "http://openweathermap.org/img/w/" + icon + ".png";
                           Picasso.with(getApplicationContext()).load(iconUrl).into(weatherImage);
                           textView.setText(weather);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                },

                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                }
        ); // request Ends

MainActivity.java 文件:

public class MainActivity extends AppCompatActivity {

 private ConstraintLayout constraintLayout;
 private Button button;
 private EditText editText;
 private ImageView weatherImage;
 private TextView textView;
 private RequestQueue requestQueue;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = findViewById(R.id.button);
        editText = findViewById(R.id.editTextTextPersonName);
        weatherImage = findViewById(R.id.imageView);
        textView = findViewById(R.id.textView2);
        constraintLayout = findViewById(R.id.constraintLayout);
        constraintLayout.setBackground(getDrawable(R.drawable.background));
        AnimationDrawable animationDrawable = (AnimationDrawable) constraintLayout.getBackground();
        animationDrawable.setExitFadeDuration(2000);
        animationDrawable.setEnterFadeDuration(2000);
        animationDrawable.start();

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String city = editText.getText().toString();
                if(TextUtils.isEmpty(city)){
                    editText.setError("Please Enter City");
                    return;
                }

                getWeatherData(city);
            }
        });


    }//OnCreate Ends


    public void getWeatherData(String city){
        requestQueue = VolleySingleton.getmInstance(getApplicationContext()).getRequestQueue();
        String url = "http://api.openweathermap.org/data/2.5/weather?q=";
        String appId = "&appid=5d2019582a736b2323e5ae971940074a";
        String readyUrl = url + city + appId;

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, readyUrl, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        JSONArray arr;
                        try {
                           arr = response.getJSONArray("weather");
                           JSONObject weatherObj = arr.getJSONObject(0);
                           String weather = weatherObj.getString("main");
                           String icon = weatherObj.getString("icon");
                           String iconUrl = "http://openweathermap.org/img/w/" + icon + ".png";
                           Picasso.with(getApplicationContext()).load(iconUrl).into(weatherImage);
                           textView.setText(weather);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        
                    }
                },

                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                }
        ); // request Ends

        requestQueue.add(request);

    }

}

调试时出现此错误:

E/ANR_LOG: >>> msg's executing time is too long
    Blocked msg = { when=-52s106ms what=0 target=android.view.ViewRootImpl$ViewRootHandler callback=android.view.View$PerformClick } , cost  = 52101 ms
    >>>Current msg List is:
E/ANR_LOG: Current msg <1>  = { when=-52s105ms what=0 target=android.view.ViewRootImpl$ViewRootHandler callback=android.view.View$UnsetPressedState }
E/ANR_LOG: Current msg <2>  = { when=-52s102ms what=0 target=android.view.ViewRootImpl$ViewRootHandler callback=android.widget.Editor$1 }
E/ANR_LOG: Current msg <3>  = { when=-52s97ms what=2 target=android.view.Choreographer$FrameHandler arg1=1 obj=android.graphics.drawable.DrawableContainer$1@8d85592 }
E/ANR_LOG: Current msg <4>  = { when=-51s787ms what=0 target=android.view.ViewRootImpl$ViewRootHandler callback=android.widget.Editor$Blink }
E/ANR_LOG: Current msg <5>  = { when=-48s881ms what=2 target=android.view.Choreographer$FrameHandler arg1=1 obj=android.graphics.drawable.AnimationDrawable@f1b1b63 }
    >>>CURRENT MSG DUMP OVER<<<

请建议应该做什么。

【问题讨论】:

    标签: java android android-volley


    【解决方案1】:

    据我所知,您应该将 asynctask 用于长任务。您可以查看本教程以获取异步任务.. https://developer.android.com/codelabs/android-training-create-asynctask?index=..%2F..%2Fandroid-training#0。之后,您可以在 doInBackground 异步任务函数上使用 getWeatherData 函数。

    【讨论】:

    • 这个。 Volley 请求阻塞,不应在 UI 线程上进行。
    • Volley 也有异步功能。对!!
    • 是的,你是对的,但是你的 onResponse 函数在 UI 线程中工作。
    【解决方案2】:

    只需将 android:usesCleartextTraffic="true" 放在清单文件中的 application 标签内

    【讨论】:

      猜你喜欢
      • 2020-07-19
      • 2018-12-12
      • 2017-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-15
      • 2010-10-15
      • 1970-01-01
      相关资源
      最近更新 更多