Home > Software design >  API output doesn't show in a TextView
API output doesn't show in a TextView

Time:04-27

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 one rrorResponse(VolleyError error) {
                error.printStackTrace();
         }
        });
        requestQueue.add(request);
    }
}

CodePudding user response:

use etCityName.getText().toString

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

CodePudding user response:

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 {
                            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 one rrorResponse(VolleyError error) {
                error.printStackTrace();
         }
        });
        requestQueue.add(request);
    }
}
  • Related