skip to Main Content

I tried to make a very simple weather app that shows the temperature of a certain city.

But when I press the button to get the temperature, the text doesn’t change.

Here is what happens if I try to get the temperature

My code:


public class MainActivity extends AppCompatActivity {

    TextView textView;
    RequestQueue requestQueue;
    Button button;
    EditText etCityName;

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

        textView = findViewById(R.id.textView);
        button = findViewById(R.id.button);
        etCityName = findViewById(R.id.etCityName);

        requestQueue = Volley.newRequestQueue(this);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jsonParse();
            }
        });
    }
    public void jsonParse() {
        String url = "https://api.openweathermap.org/data/2.5/weather?q=" + etCityName.getText() + "&appid=token";

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) { ;
                        try {
                            JSONArray jsonArray = response.getJSONArray("main");

                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject url = jsonArray.getJSONObject(i);

                                String temp = url.getString("temp");
                                textView.setText(temp);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
         }
        });
        requestQueue.add(request);
    }
}

2

Answers


  1. use etCityName.getText().toString

    instead of etCityName.getText() in your jsonparse() method

    Login or Signup to reply.
  2. public class MainActivity extends AppCompatActivity {
    
        TextView textView;
        RequestQueue requestQueue;
        Button button;
        EditText etCityName;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            textView = findViewById(R.id.textView);
            button = findViewById(R.id.button);
            etCityName = findViewById(R.id.etCityName);
    
            requestQueue = Volley.newRequestQueue(this);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (etCityName.getText().toString().trim().isEmpty()) {
                     etCityName.setError("Required");
                    }else{
                          jsonParse();
                    }
                }
            });
        }
        public void jsonParse() {
            String url = "https://api.openweathermap.org/data/2.5/weather?q=" + etCityName.getText().toString().trim() + "&appid=token";
    
            JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) { ;
                            try {
                                    String temp = response.getDouble("temp").toString();
                                    textView.setText(temp);
                                
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
    
                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    error.printStackTrace();
             }
            });
            requestQueue.add(request);
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search