Weather App

In questo tutorial andremo a progettare una semplice app meteo con un EditText per digitare la località, un Button e un TextView:





<?xml version=”1.0″ encoding=”utf-8″?>

<LinearLayout
xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
xmlns:tools=”http://schemas.android.com/tools&#8221;
android:gravity=”center”
android:background=”@mipmap/background1″
android:orientation=”vertical”
tools:context=”.MainActivity”>

<TextView
android:id=”@+id/textView”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Hello, world!”
android:textAppearance=”?android:attr/textAppearanceMedium”/>
<EditText
android:id=”@+id/editText”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Hello, world!”
android:textAppearance=”?android:attr/textAppearanceMedium”/>
<Button
android:id=”@+id/button”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Hello, world!”
android:textAppearance=”?android:attr/textAppearanceMedium”/>
</LinearLayout>



Per lo sfondo utiliziamo una risorsa disegnabile:
<?xml version=”1.0″ encoding=”utf-8″?>




<shape xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
    android:shape=”rectangle” >


   

<gradient
        android:angle=”90″
        android:endColor=”#3030FF”
        android:centerColor=”#3D98FF”
        android:startColor=”#A1CEFF”
        android:type=”linear” />
</shape>





Adesso andiamo su OpenWeatherMap, un servizio online che fornisce dati meteorologici.
e ottieni la tua chiave API. Quindi inserisci l’
URL nel mainJava come nell’esempio:

package com.exsample.weatherapp.activities;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.*;
import android.widget.Button;
import android.os.Bundle;
import android.view.View;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.VolleyError;
import androidx.appcompat.app.AppCompatActivity;
import com.exsample.weatherapp.R;





public class MainActivity extends AppCompatActivity {
EditText editText;
Button button;
TextView textView;
String api=”https://api.openweathermap.org/data/2.5/weather?q=&#8221;;




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

        button = findViewById(R.id.button);
        editText = findViewById(R.id.editText);
        textView = findViewById(R.id.textView);

        button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String myURL = api + editText.getText().toString() ;
JsonObjectRequest jsonObject= new JsonObjectRequest(Request.Method.GET, myURL, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject jsonObject) {

try{
String info = jsonObject.getString(“weather”);
JSONArray js = new JSONArray(info);

for(int i = 0; i < js.length(); i++)
{
JSONObject json = js.getJSONObject(i);

}






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

        new Response.ErrorListener()
                        {
        @Override
        public void onErrorResponse(VolleyError error) {
                              
                            }
                        }
} );