Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9242127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:31:03+00:00 2026-06-18T08:31:03+00:00

I am learning Android development now and I wanted to try out whatever basic

  • 0

I am learning Android development now and I wanted to try out whatever basic programs I’ve done in iOS development. The problem is to get the weather condition of a place and set the condition in TextView.

Code

package com.bh.weather;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

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

    protected void onPostExecute(String json) {
        try {
            JSONObject jsonObject = new JSONObject(json);
            String value = jsonObject.getJSONObject("data")
                    .getJSONArray("current_condition").getJSONObject(0)
                    .getJSONArray("weatherDesc").getJSONObject(0)
                    .getString("value");
            Log.d("bh", value);
            MainActivity m = new MainActivity();
            m.setTextView(value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected String doInBackground(String... urlTojson) {
        String json = new String();
        try {
            DefaultHttpClient defaultClient = new DefaultHttpClient();
            HttpGet httpGetRequest = new HttpGet(urlTojson[0]);
            HttpResponse httpResponse = defaultClient.execute(httpGetRequest);
            BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), "UTF-8"));
            json = reader.readLine();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }
}

public class MainActivity extends Activity implements OnClickListener {

    private TextView tv = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(this);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
    //changed the API key here to xxxxxxxxxxxxxxxx
        new WeatherTask().execute("http://free.worldweatheronline.com/feed/weather.ashx?q=Bangalore,India&format=json&num_of_days=1&key=xxxxxxxxxxxxxxxxx");
    }

    public void setTextView(String v) {
        Log.d("bh","Inside setTextView:"+v);
        if(v.equals("")) {
            Log.d("bh","Value not received");
        }
        else {
            tv = (TextView) findViewById(R.id.textView3);
            tv.setText(v);
        }
    }
}

The resulting JSON looks like this (jsonviewer.stack.hu can be used for viewing) :

{
  "data": {
    "current_condition": [
      {
        "cloudcover": "0",
        "humidity": "30",
        "observation_time": "01:29 PM",
        "precipMM": "0.0",
        "pressure": "1017",
        "temp_C": "25",
        "temp_F": "77",
        "visibility": "10",
        "weatherCode": "113",
        "weatherDesc": [
          {
            "value": "Clear"
          }
        ],
        "weatherIconUrl": [
          {
            "value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0008_clear_sky_night.png"
          }
        ],
        "winddir16Point": "E",
        "winddirDegree": "90",
        "windspeedKmph": "13",
        "windspeedMiles": "8"
      }
    ],
    "request": [
      {
        "query": "Bangalore, India",
        "type": "City"
      }
    ],
    "weather": [
      {
        "date": "2013-01-25",
        "precipMM": "0.0",
        "tempMaxC": "29",
        "tempMaxF": "84",
        "tempMinC": "15",
        "tempMinF": "59",
        "weatherCode": "113",
        "weatherDesc": [
          {
            "value": "Sunny"
          }
        ],
        "weatherIconUrl": [
          {
            "value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0001_sunny.png"
          }
        ],
        "winddir16Point": "E",
        "winddirDegree": "97",
        "winddirection": "E",
        "windspeedKmph": "17",
        "windspeedMiles": "11"
      }
    ]
  }
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="22dp"
        android:text="@string/title" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="32dp"
        android:layout_marginTop="30dp"
        android:text="@string/place_name"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="36dp"
        android:layout_marginLeft="0dp"
        android:text="@string/weather_condition" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerVertical="true"
        android:layout_marginTop="140dp"
        android:layout_marginLeft="120dp"
        android:text="@string/show_weather" />

</RelativeLayout>

As you can see from the code, I get the value “Clear” after parsing (USES Internet permission set). But for some reason, this is not getting set in the TextView. I don’t know what mistake I have done here. Logcat is showing the logged values properly. Please help.

Cheers.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:31:04+00:00Added an answer on June 18, 2026 at 8:31 am

    try

     class WeatherTask extends AsyncTask<String, Void, String> {
        MainActivity mActivity;
         public WeatherTask(MainActivity mActivity){
    
              this.mActivity=mActivity;
                      }
    
    protected void onPostExecute(String json) {
        try {
            JSONObject jsonObject = new JSONObject(json);
            String value = jsonObject.getJSONObject("data")
                    .getJSONArray("current_condition").getJSONObject(0)
                    .getJSONArray("weatherDesc").getJSONObject(0)
                    .getString("value");
            Log.d("bh", value);
    
            mActivity.setTextView(value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    @Override
    protected String doInBackground(String... urlTojson) {
        String json = new String();
        try {
            DefaultHttpClient defaultClient = new DefaultHttpClient();
            HttpGet httpGetRequest = new HttpGet(urlTojson[0]);
            HttpResponse httpResponse = defaultClient.execute(httpGetRequest);
            BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), "UTF-8"));
            json = reader.readLine();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }
    }
    
     public class MainActivity extends Activity implements OnClickListener {
    
    private TextView tv = null;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(this);
    
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    @Override
    public void onClick(View v) {
    //changed the API key here to xxxxxxxxxxxxxxxx
        new WeatherTask(this).execute("http://free.worldweatheronline.com/feed/weather.ashx?q=Bangalore,India&format=json&num_of_days=1&key=xxxxxxxxxxxxxxxxx");
    }
    
    public void setTextView(String v) {
        Log.d("bh","Inside setTextView:"+v);
        if(v.equals("")) {
            Log.d("bh","Value not received");
        }
        else {
            tv = (TextView) findViewById(R.id.textView3);
            tv.setText(v);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm learning Android development. I have a basic app running on the Gingerbread emulator,
I've been learning android development and been trying to get the app on my
I've been learning android development and been trying to get the app on my
I am new to Android development and currently learning to design for a basic
I am trying to make simple notepad application for learning android development. So, to
I'm learning Android and I've done a custom control. When I put it in
I'm familiar with C++ but never used Java. I've just started learning Android development.
I am still learning the android apps development and after I started making a
I've been learning android development (in Eclipse) the past week. I'm following online courses
I'm learning Android development using Pro Android Web Apps (Apress, Auths: Damon Oehlman &

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.