-1

I am working on an Android app in which I am making a weather app. The application opens api data from a JSON and displays this information. More specifically it takes the user input of a city or zip code and adds this info to the URL for the API and then executes the URL.

MainActivity.java

package com.shanehampcsci3660.weather;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

//import static com.shanehampcsci3660.weather.GetJsonData.*;

public class MainActivity extends AppCompatActivity
{
    public static TextView tempTV, jsonTV;
    public EditText cityORZipET;
    private Button getWeather;
    public String zip, cityOrZip;

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

        tempTV = (TextView) findViewById(R.id.tempTV);
        jsonTV = (TextView) findViewById(R.id.jsonFeedTV);

        cityORZipET = (EditText) findViewById(R.id.cityORZipET);

        getWeather = (Button) findViewById(R.id.getWeatherButton);
        //cityOrZip = cityORZipET.getText().toString();

        getWeather.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {

                GetJsonData getJsonData = new GetJsonData();
                //getJsonData.execute();
                cityOrZip = cityORZipET.getText().toString();
                getJsonData.execute("https://api.openweathermap.org/data/2.5/weather?q=" + cityOrZip +"&appid=x");
                jsonTV.setText(cityOrZip);

                /*if(cityOrZip.equals(""))
                {
                    getJsonData.execute("https://api.openweathermap.org/data/2.5/weather?q=" + cityOrZip +"&appid=x");

                }
                else
                {
                    zip = "30528";
                    getJsonData.execute("https://api.openweathermap.org/data/2.5/weather?q=" + zip + "&appid=x");
                }*/


            }
        });

    }

}

GetJsonData.java

package com.shanehampcsci3660.weather;

import android.os.AsyncTask;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class GetJsonData extends AsyncTask<Void, Void, Void>
{


    public String data= "", line = "", name = "";
    double temp, minTemp, maxTemp;




    @Override
    protected Void doInBackground(Void... voids)
    {
        try
        {
            URL url = new URL("https://api.openweathermap.org/data/2.5/weather?q=30528&appid=id");

            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

            while(line != null)
            {
                line = bufferedReader.readLine();
                data = data + line;
            }

            JSONObject jsonObject = new JSONObject(data);
            JSONObject main = jsonObject.getJSONObject("main");

            name = jsonObject.getString("name");
            temp = main.getDouble("temp");
            minTemp = main.getDouble("min_temp");
            maxTemp = main.getDouble("max_temp");

            /*JSONObject jsonObject = new JSONObject(result);
            JSONObject jsonData = new JSONObject(jsonObject.getString("main"));
            String weatherInfo = jsonObject.getString("weather");
            JSONArray jsonArray = new JSONArray(weatherInfo);
            String description, icon, iconURI;
            for(int i = 0; i < jsonArray.length(); i++)
            {
                JSONObject jsonData1 = jsonArray.getJSONObject(i);
                description = jsonData1.getString("description");
                MainActivityController.description.setText(description);
                icon = jsonData1.getString("icon");
                iconURI = "http://openweathermap.org/img/w/" + icon + ".png";
                Picasso.get().load(iconURI).into(MainActivityController.conditionImageView);
            }*/

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


        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid)
    {
        super.onPostExecute(aVoid);

        //MainActivity.jsonTV.setText(data);
        MainActivity.tempTV.setText("City:\t" + name + "\tTemp:\t" + temp);

        Log.d("Json Feed", name + "Temp: " + temp);
    }

    public static void execute(String s) {
    }
}

My issue is that no matter where I put...

if(cityOrZip.equals(""))
                    {
                        getJsonData.execute("https://api.openweathermap.org/data/2.5/weather?q=" + cityOrZip +"&appid=id");

                    }
                    else
                    {
                        zip = "30528";
                        getJsonData.execute("https://api.openweathermap.org/data/2.5/weather?q=" + zip + "&appid=id");
                    }

...It will only show the data of the default Url opened in GetJsonData.java. My question is what am I doing wrong and what should I correct in order to make my app work with the user input like it should.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
shamp113
  • 11
  • 3

2 Answers2

0

you are calling an GetJsonData.execute with the url that contains the client data, but it has no code in it. Looking at your current code the zip that the client inputs does not get stored into the worker class.

tstoev
  • 1,415
  • 11
  • 12
0

This is because you always use the same URL in GetJsonData class. According to question What arguments are passed into AsyncTask? your class declaration should look like:

public class GetJsonData extends AsyncTask<String, Void, YourPojo> {

    @Override
    protected YourPojo doInBackground(String... urls)
    {
        try
        {
            URL url = new URL(urls[0]);
            ...
        } catch (Exception e) {
           handle(e);
        }
   }
}

where YourPojo is a class you need to create to store all properties you need to read from JSON payload:

class YourPojo {
    private String data;
    private String line;
    private String name;
    private double temp;
    private double minTemp
    private double maxTemp;

    // getters, setters
}
Michał Ziober
  • 37,175
  • 18
  • 99
  • 146