1

I am new to android Please help me with this problem.

I am fetching JSON via volley API but it is showing too much work on the Main thread.

Everything is working fine but when comes to making a request nothing happens. I have mentioned the Error message below the code file. It's something about performance but I don't know how to resolve it. Please Help.

When I debugged it , I found after this the code is not executing,

 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 file:

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);

    }

}

While Debugging I got this Error :

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<<<

Please suggest What should be done.

2 Answers2

1

As i know, you should use asynctask for long task. you can look this tutorial for async task.. https://developer.android.com/codelabs/android-training-create-asynctask?index=..%2F..%2Fandroid-training#0. after that you can use your getWeatherData Function on doInBackground async task function.

Boma Anjang
  • 132
  • 1
  • 10
0

Just put android:usesCleartextTraffic="true" inside application tag inside the manifest file