0

it is basically a beginner-level weather app. this is my code in the main activity of the app.
every library is imported.i checked in various places .i have implemented exactly as they said but still, nothing seems to be displaying in the app Also, dependency was implemented which was ---> implementation 'com.android.volley:volley:1.1.1'.

package susinth.josh.weatherapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

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

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class MainActivity extends AppCompatActivity {
    private TextView text_temp;
    private TextView text_city;
    private TextView text_description;
    private TextView text_date;


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

        text_temp=findViewById(R.id.txtTemp);
        text_city=findViewById(R.id.txtCity);
        text_description=findViewById(R.id.txtDesc);
        text_date=findViewById(R.id.txtDate);

        find_weather();
    }



    public void find_weather() {
        String url="http://api.openweathermap.org/data/2.5/weather?q=London&appid=01138a7db759c13b7b0a1390ed862fff&units=metric";
        JsonObjectRequest jor= new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {

                try {
                    JSONObject main_object=response.getJSONObject("main");
                    JSONArray weatherArray=response.getJSONArray("weather");
                    JSONObject object=weatherArray.getJSONObject(0);

                    String temp= String.valueOf(main_object.getDouble("temp"));
                    String description=object.getString("description");
                    String city=response.getString("name");
                    System.out.println(temp);
                    Calendar calendar=Calendar.getInstance();
                    SimpleDateFormat sdf=new SimpleDateFormat("EEEE-MM-dd");
                    String formattedDate=sdf.format(calendar.getTime());

                    text_date.append(formattedDate);
                    text_city.setText(city);
                    text_description.setText(description);
                    text_temp.setText(temp);


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

                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });
        RequestQueue queue= Volley.newRequestQueue(this);
        queue.add(jor);
    }
}

my json data

{
     "coord": {
       "lon": -0.13,
       "lat": 51.51
     },
     "weather": [
       {
         "id": 300,
         "main": "Drizzle",
         "description": "light intensity drizzle",
         "icon": "09d"
       }
     ],
     "base": "stations",
     "main": {
       "temp": 280.32,
       "pressure": 1012,
       "humidity": 81,
       "temp_min": 279.15,
       "temp_max": 281.15
     },
     "visibility": 10000,
     "wind": {
       "speed": 4.1,
       "deg": 80
     },
     "clouds": {
       "all": 90
     },
     "dt": 1485789600,
     "sys": {
       "type": 1,
       "id": 5091,
       "message": 0.0103,
       "country": "GB",
       "sunrise": 1485762037,
       "sunset": 1485794875
     },
     "id": 2643743,
     "name": "London",
     "cod": 200
     }
Daan
  • 475
  • 5
  • 17

1 Answers1

0

Your code setup looks good and reproducing this with a clean Android/Java project there are 2 possible problems your project might face. All info was printed in the Logcat console by this code:

public void onErrorResponse(VolleyError error) {
    error.printStackTrace();
}

1st possible problem

Initially this Error is returned: com.android.volley.NoConnectionError: java.io.IOException: Cleartext HTTP traffic to api.openweathermap.org not permitted;

This Error can be resolved by changing the API url to use the HTTPS instead of HTTP protocol. So you end up with:

String url="http://api.openweathermap.org/data/2.5/weather?q=London&appid=01138a7db759c13b7b0a1390ed862fff&units=metric";

Read more info on the Cleartext error here: Android 8: Cleartext HTTP traffic not permitted

2nd possible problem

Make sure you added you added the Internet permission to your AndroidManifest.xml:

<uses-permission android:name="android.permission.INTERNET" />

Assuming your layout is working OK, this should fix showing the data on screen.

Daan
  • 475
  • 5
  • 17