【发布时间】: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