Textview is not displaying











up vote
-2
down vote

favorite












The textview is not displaying the value I need. I am trying to get a weather forecast shown inside of it. However, when I try to, the app does not display anything at all.



<TextView
android:id="@+id/degree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Loading..."
android:textSize="60sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />


Down is my main activity java class



public class MainActivity extends AppCompatActivity {

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

TextView tv = findViewById(R.id.degree);

WeatherTask task = new WeatherTask(tv);
task.execute("moscow");
}
}


Here is the main java class that gets a forecast for a selected city



public class WeatherTask extends AsyncTask<String, Void, String> {

private static final String API_KEY = "06c7d6512a914b3029937dd444283ff0";
private final TextView textView;


public WeatherTask(TextView tv){
this.textView = tv;
}

@Override
protected String doInBackground(String... cities) {
String weather = "";

try {
String urlString = String.format(
"http://api.openweathermap.org/data/2.5/weather?q=%s,uz&units=%s&appid=%s",
cities[0], "metric", API_KEY
);

URL url = new URL(urlString);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

urlConnection.connect();

InputStream stream = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));

StringBuilder builder = new StringBuilder();
String inputString;

while ((inputString = bufferedReader.readLine()) != null) {
builder.append(inputString);
}

String response = builder.toString();

JSONObject topLevel = new JSONObject(response);

JSONObject main = topLevel.getJSONObject("main");
weather = String.valueOf(main.getDouble("temp"));
urlConnection.disconnect();

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

return weather;
}

@Override
protected void onPostExecute(String weather)
{
this.textView.setText(weather);
}
}


Can somebody please explain why textview is not shown on main activity?



Thank you










share|improve this question


























    up vote
    -2
    down vote

    favorite












    The textview is not displaying the value I need. I am trying to get a weather forecast shown inside of it. However, when I try to, the app does not display anything at all.



    <TextView
    android:id="@+id/degree"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Loading..."
    android:textSize="60sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />


    Down is my main activity java class



    public class MainActivity extends AppCompatActivity {

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

    TextView tv = findViewById(R.id.degree);

    WeatherTask task = new WeatherTask(tv);
    task.execute("moscow");
    }
    }


    Here is the main java class that gets a forecast for a selected city



    public class WeatherTask extends AsyncTask<String, Void, String> {

    private static final String API_KEY = "06c7d6512a914b3029937dd444283ff0";
    private final TextView textView;


    public WeatherTask(TextView tv){
    this.textView = tv;
    }

    @Override
    protected String doInBackground(String... cities) {
    String weather = "";

    try {
    String urlString = String.format(
    "http://api.openweathermap.org/data/2.5/weather?q=%s,uz&units=%s&appid=%s",
    cities[0], "metric", API_KEY
    );

    URL url = new URL(urlString);
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

    urlConnection.connect();

    InputStream stream = new BufferedInputStream(urlConnection.getInputStream());
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));

    StringBuilder builder = new StringBuilder();
    String inputString;

    while ((inputString = bufferedReader.readLine()) != null) {
    builder.append(inputString);
    }

    String response = builder.toString();

    JSONObject topLevel = new JSONObject(response);

    JSONObject main = topLevel.getJSONObject("main");
    weather = String.valueOf(main.getDouble("temp"));
    urlConnection.disconnect();

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

    return weather;
    }

    @Override
    protected void onPostExecute(String weather)
    {
    this.textView.setText(weather);
    }
    }


    Can somebody please explain why textview is not shown on main activity?



    Thank you










    share|improve this question
























      up vote
      -2
      down vote

      favorite









      up vote
      -2
      down vote

      favorite











      The textview is not displaying the value I need. I am trying to get a weather forecast shown inside of it. However, when I try to, the app does not display anything at all.



      <TextView
      android:id="@+id/degree"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Loading..."
      android:textSize="60sp"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintLeft_toLeftOf="parent"
      app:layout_constraintRight_toRightOf="parent"
      app:layout_constraintTop_toTopOf="parent" />


      Down is my main activity java class



      public class MainActivity extends AppCompatActivity {

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

      TextView tv = findViewById(R.id.degree);

      WeatherTask task = new WeatherTask(tv);
      task.execute("moscow");
      }
      }


      Here is the main java class that gets a forecast for a selected city



      public class WeatherTask extends AsyncTask<String, Void, String> {

      private static final String API_KEY = "06c7d6512a914b3029937dd444283ff0";
      private final TextView textView;


      public WeatherTask(TextView tv){
      this.textView = tv;
      }

      @Override
      protected String doInBackground(String... cities) {
      String weather = "";

      try {
      String urlString = String.format(
      "http://api.openweathermap.org/data/2.5/weather?q=%s,uz&units=%s&appid=%s",
      cities[0], "metric", API_KEY
      );

      URL url = new URL(urlString);
      HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

      urlConnection.connect();

      InputStream stream = new BufferedInputStream(urlConnection.getInputStream());
      BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));

      StringBuilder builder = new StringBuilder();
      String inputString;

      while ((inputString = bufferedReader.readLine()) != null) {
      builder.append(inputString);
      }

      String response = builder.toString();

      JSONObject topLevel = new JSONObject(response);

      JSONObject main = topLevel.getJSONObject("main");
      weather = String.valueOf(main.getDouble("temp"));
      urlConnection.disconnect();

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

      return weather;
      }

      @Override
      protected void onPostExecute(String weather)
      {
      this.textView.setText(weather);
      }
      }


      Can somebody please explain why textview is not shown on main activity?



      Thank you










      share|improve this question













      The textview is not displaying the value I need. I am trying to get a weather forecast shown inside of it. However, when I try to, the app does not display anything at all.



      <TextView
      android:id="@+id/degree"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Loading..."
      android:textSize="60sp"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintLeft_toLeftOf="parent"
      app:layout_constraintRight_toRightOf="parent"
      app:layout_constraintTop_toTopOf="parent" />


      Down is my main activity java class



      public class MainActivity extends AppCompatActivity {

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

      TextView tv = findViewById(R.id.degree);

      WeatherTask task = new WeatherTask(tv);
      task.execute("moscow");
      }
      }


      Here is the main java class that gets a forecast for a selected city



      public class WeatherTask extends AsyncTask<String, Void, String> {

      private static final String API_KEY = "06c7d6512a914b3029937dd444283ff0";
      private final TextView textView;


      public WeatherTask(TextView tv){
      this.textView = tv;
      }

      @Override
      protected String doInBackground(String... cities) {
      String weather = "";

      try {
      String urlString = String.format(
      "http://api.openweathermap.org/data/2.5/weather?q=%s,uz&units=%s&appid=%s",
      cities[0], "metric", API_KEY
      );

      URL url = new URL(urlString);
      HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

      urlConnection.connect();

      InputStream stream = new BufferedInputStream(urlConnection.getInputStream());
      BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));

      StringBuilder builder = new StringBuilder();
      String inputString;

      while ((inputString = bufferedReader.readLine()) != null) {
      builder.append(inputString);
      }

      String response = builder.toString();

      JSONObject topLevel = new JSONObject(response);

      JSONObject main = topLevel.getJSONObject("main");
      weather = String.valueOf(main.getDouble("temp"));
      urlConnection.disconnect();

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

      return weather;
      }

      @Override
      protected void onPostExecute(String weather)
      {
      this.textView.setText(weather);
      }
      }


      Can somebody please explain why textview is not shown on main activity?



      Thank you







      android android-studio






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 at 21:30









      Incognito

      67




      67
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote













          http://api.openweathermap.org/data/2.5/weather?q=%s,uz&units=%s&appid=%s"  


          should be:



          http://api.openweathermap.org/data/2.5/weather?q=%s&units=%s&appid=%s"


          if you use first request, there should response like:



          {
          "cod": "404",
          "message": "city not found"
          }


          because it will concate 'cities[0]' and ',uz' as first parameter.






          share|improve this answer




























            up vote
            0
            down vote













            I tried your URL, and it's returning a JSON response with different structure - it says unknown city. Perhaps you want to remove ,uz from your query:



            "http://api.openweathermap.org/data/2.5/weather?q=%s&units=%s&appid=%s"


            You should also check your log, e.printStacktrace would have provided clear error message. Also I checked that uz is a country code for Uzbekistan, but I'm sure Moscow is in Russia, so maybe that's where it fails and explains why your TextView isn't getting anything.






            share|improve this answer























            • But at least it should be showing loading.Also, I did not get the point about ,uv. Can you explain what you meant by that?
              – Incognito
              Nov 14 at 22:00












            • Sorry I have no idea what ,uv means, you should probably check with the API. It doesn't make sense to me for having it as query parameter either. Anyway with it, I get {"cod":"404","message":"city not found"} from response, which will fail JSONObject main = topLevel.getJSONObject("main") in your code, which will cause e.printStackTrace so pretty sure you should be getting something in your log.
              – Aaron
              Nov 14 at 22:08










            • @Incognito Sorry I meant ,uz. I bet you probably copied and pasted the URL somewhere, and ,uz is a country code for Uzbekistan. The last time I checked Moscow was in Russia... so just that it out from your URL.
              – Aaron
              Nov 14 at 22:18











            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53309026%2ftextview-is-not-displaying%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            http://api.openweathermap.org/data/2.5/weather?q=%s,uz&units=%s&appid=%s"  


            should be:



            http://api.openweathermap.org/data/2.5/weather?q=%s&units=%s&appid=%s"


            if you use first request, there should response like:



            {
            "cod": "404",
            "message": "city not found"
            }


            because it will concate 'cities[0]' and ',uz' as first parameter.






            share|improve this answer

























              up vote
              0
              down vote













              http://api.openweathermap.org/data/2.5/weather?q=%s,uz&units=%s&appid=%s"  


              should be:



              http://api.openweathermap.org/data/2.5/weather?q=%s&units=%s&appid=%s"


              if you use first request, there should response like:



              {
              "cod": "404",
              "message": "city not found"
              }


              because it will concate 'cities[0]' and ',uz' as first parameter.






              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                http://api.openweathermap.org/data/2.5/weather?q=%s,uz&units=%s&appid=%s"  


                should be:



                http://api.openweathermap.org/data/2.5/weather?q=%s&units=%s&appid=%s"


                if you use first request, there should response like:



                {
                "cod": "404",
                "message": "city not found"
                }


                because it will concate 'cities[0]' and ',uz' as first parameter.






                share|improve this answer












                http://api.openweathermap.org/data/2.5/weather?q=%s,uz&units=%s&appid=%s"  


                should be:



                http://api.openweathermap.org/data/2.5/weather?q=%s&units=%s&appid=%s"


                if you use first request, there should response like:



                {
                "cod": "404",
                "message": "city not found"
                }


                because it will concate 'cities[0]' and ',uz' as first parameter.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 14 at 22:11









                navylover

                3,14421118




                3,14421118
























                    up vote
                    0
                    down vote













                    I tried your URL, and it's returning a JSON response with different structure - it says unknown city. Perhaps you want to remove ,uz from your query:



                    "http://api.openweathermap.org/data/2.5/weather?q=%s&units=%s&appid=%s"


                    You should also check your log, e.printStacktrace would have provided clear error message. Also I checked that uz is a country code for Uzbekistan, but I'm sure Moscow is in Russia, so maybe that's where it fails and explains why your TextView isn't getting anything.






                    share|improve this answer























                    • But at least it should be showing loading.Also, I did not get the point about ,uv. Can you explain what you meant by that?
                      – Incognito
                      Nov 14 at 22:00












                    • Sorry I have no idea what ,uv means, you should probably check with the API. It doesn't make sense to me for having it as query parameter either. Anyway with it, I get {"cod":"404","message":"city not found"} from response, which will fail JSONObject main = topLevel.getJSONObject("main") in your code, which will cause e.printStackTrace so pretty sure you should be getting something in your log.
                      – Aaron
                      Nov 14 at 22:08










                    • @Incognito Sorry I meant ,uz. I bet you probably copied and pasted the URL somewhere, and ,uz is a country code for Uzbekistan. The last time I checked Moscow was in Russia... so just that it out from your URL.
                      – Aaron
                      Nov 14 at 22:18















                    up vote
                    0
                    down vote













                    I tried your URL, and it's returning a JSON response with different structure - it says unknown city. Perhaps you want to remove ,uz from your query:



                    "http://api.openweathermap.org/data/2.5/weather?q=%s&units=%s&appid=%s"


                    You should also check your log, e.printStacktrace would have provided clear error message. Also I checked that uz is a country code for Uzbekistan, but I'm sure Moscow is in Russia, so maybe that's where it fails and explains why your TextView isn't getting anything.






                    share|improve this answer























                    • But at least it should be showing loading.Also, I did not get the point about ,uv. Can you explain what you meant by that?
                      – Incognito
                      Nov 14 at 22:00












                    • Sorry I have no idea what ,uv means, you should probably check with the API. It doesn't make sense to me for having it as query parameter either. Anyway with it, I get {"cod":"404","message":"city not found"} from response, which will fail JSONObject main = topLevel.getJSONObject("main") in your code, which will cause e.printStackTrace so pretty sure you should be getting something in your log.
                      – Aaron
                      Nov 14 at 22:08










                    • @Incognito Sorry I meant ,uz. I bet you probably copied and pasted the URL somewhere, and ,uz is a country code for Uzbekistan. The last time I checked Moscow was in Russia... so just that it out from your URL.
                      – Aaron
                      Nov 14 at 22:18













                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    I tried your URL, and it's returning a JSON response with different structure - it says unknown city. Perhaps you want to remove ,uz from your query:



                    "http://api.openweathermap.org/data/2.5/weather?q=%s&units=%s&appid=%s"


                    You should also check your log, e.printStacktrace would have provided clear error message. Also I checked that uz is a country code for Uzbekistan, but I'm sure Moscow is in Russia, so maybe that's where it fails and explains why your TextView isn't getting anything.






                    share|improve this answer














                    I tried your URL, and it's returning a JSON response with different structure - it says unknown city. Perhaps you want to remove ,uz from your query:



                    "http://api.openweathermap.org/data/2.5/weather?q=%s&units=%s&appid=%s"


                    You should also check your log, e.printStacktrace would have provided clear error message. Also I checked that uz is a country code for Uzbekistan, but I'm sure Moscow is in Russia, so maybe that's where it fails and explains why your TextView isn't getting anything.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 14 at 22:44

























                    answered Nov 14 at 21:39









                    Aaron

                    1,6131212




                    1,6131212












                    • But at least it should be showing loading.Also, I did not get the point about ,uv. Can you explain what you meant by that?
                      – Incognito
                      Nov 14 at 22:00












                    • Sorry I have no idea what ,uv means, you should probably check with the API. It doesn't make sense to me for having it as query parameter either. Anyway with it, I get {"cod":"404","message":"city not found"} from response, which will fail JSONObject main = topLevel.getJSONObject("main") in your code, which will cause e.printStackTrace so pretty sure you should be getting something in your log.
                      – Aaron
                      Nov 14 at 22:08










                    • @Incognito Sorry I meant ,uz. I bet you probably copied and pasted the URL somewhere, and ,uz is a country code for Uzbekistan. The last time I checked Moscow was in Russia... so just that it out from your URL.
                      – Aaron
                      Nov 14 at 22:18


















                    • But at least it should be showing loading.Also, I did not get the point about ,uv. Can you explain what you meant by that?
                      – Incognito
                      Nov 14 at 22:00












                    • Sorry I have no idea what ,uv means, you should probably check with the API. It doesn't make sense to me for having it as query parameter either. Anyway with it, I get {"cod":"404","message":"city not found"} from response, which will fail JSONObject main = topLevel.getJSONObject("main") in your code, which will cause e.printStackTrace so pretty sure you should be getting something in your log.
                      – Aaron
                      Nov 14 at 22:08










                    • @Incognito Sorry I meant ,uz. I bet you probably copied and pasted the URL somewhere, and ,uz is a country code for Uzbekistan. The last time I checked Moscow was in Russia... so just that it out from your URL.
                      – Aaron
                      Nov 14 at 22:18
















                    But at least it should be showing loading.Also, I did not get the point about ,uv. Can you explain what you meant by that?
                    – Incognito
                    Nov 14 at 22:00






                    But at least it should be showing loading.Also, I did not get the point about ,uv. Can you explain what you meant by that?
                    – Incognito
                    Nov 14 at 22:00














                    Sorry I have no idea what ,uv means, you should probably check with the API. It doesn't make sense to me for having it as query parameter either. Anyway with it, I get {"cod":"404","message":"city not found"} from response, which will fail JSONObject main = topLevel.getJSONObject("main") in your code, which will cause e.printStackTrace so pretty sure you should be getting something in your log.
                    – Aaron
                    Nov 14 at 22:08




                    Sorry I have no idea what ,uv means, you should probably check with the API. It doesn't make sense to me for having it as query parameter either. Anyway with it, I get {"cod":"404","message":"city not found"} from response, which will fail JSONObject main = topLevel.getJSONObject("main") in your code, which will cause e.printStackTrace so pretty sure you should be getting something in your log.
                    – Aaron
                    Nov 14 at 22:08












                    @Incognito Sorry I meant ,uz. I bet you probably copied and pasted the URL somewhere, and ,uz is a country code for Uzbekistan. The last time I checked Moscow was in Russia... so just that it out from your URL.
                    – Aaron
                    Nov 14 at 22:18




                    @Incognito Sorry I meant ,uz. I bet you probably copied and pasted the URL somewhere, and ,uz is a country code for Uzbekistan. The last time I checked Moscow was in Russia... so just that it out from your URL.
                    – Aaron
                    Nov 14 at 22:18


















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53309026%2ftextview-is-not-displaying%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

                    How to change which sound is reproduced for terminal bell?

                    Can I use Tabulator js library in my java Spring + Thymeleaf project?