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
android android-studio
add a comment |
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
android android-studio
add a comment |
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
android android-studio
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
android android-studio
asked Nov 14 at 21:30
Incognito
67
67
add a comment |
add a comment |
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.
add a comment |
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.
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 failJSONObject main = topLevel.getJSONObject("main")
in your code, which will causee.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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 14 at 22:11
navylover
3,14421118
3,14421118
add a comment |
add a comment |
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.
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 failJSONObject main = topLevel.getJSONObject("main")
in your code, which will causee.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
add a comment |
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.
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 failJSONObject main = topLevel.getJSONObject("main")
in your code, which will causee.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
add a comment |
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.
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.
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 failJSONObject main = topLevel.getJSONObject("main")
in your code, which will causee.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
add a comment |
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 failJSONObject main = topLevel.getJSONObject("main")
in your code, which will causee.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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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