Skip to content

Instantly share code, notes, and snippets.

@Smarticles101
Created June 5, 2020 17:56
Show Gist options
  • Save Smarticles101/e8e7308d3bf0e3146cd34c38618b44b9 to your computer and use it in GitHub Desktop.
Save Smarticles101/e8e7308d3bf0e3146cd34c38618b44b9 to your computer and use it in GitHub Desktop.
Proof of android development
package com.example.weatherapp;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import com.google.gson.Gson;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onGetTemp(View v) {
EditText editText = findViewById(R.id.editText);
String city = editText.getText().toString();
Runnable r = new WeatherTask(this, city);
Thread t = new Thread(r);
t.start();
}
public void onGetForecast(View v) {
EditText editText = findViewById(R.id.editText);
String city = editText.getText().toString();
Runnable r = new ForecastTask(this, city);
Thread t = new Thread(r);
t.start();
}
}
class WeatherTask implements Runnable {
private String city;
private Activity parentActivity;
WeatherTask(Activity parent, String city) {
this.city = city;
parentActivity = parent;
}
@Override
public void run() {
Log.d("WeatherTask", "Getting weather for " + city);
String url = "";
try {
url = "https://api.openweathermap.org/data/2.5/weather?q=" + URLEncoder.encode(city, "UTF-8") + "&apiKey=830472d14b275cc781bbd5a0fe04e376";
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
WeatherConditions weatherConditions = null;
try {
InputStream response = new URL(url).openStream();
Reader reader = new InputStreamReader(response);
weatherConditions = new Gson().fromJson(reader, WeatherConditions.class);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Log.d("WeatherTask", "Current temp in " + city + ": " + weatherConditions.measurements.temp);
WeatherConditions finalWeatherConditions = weatherConditions;
parentActivity.runOnUiThread(() -> {
Toast t = Toast.makeText(parentActivity, "Temperature in " + city + " is " + finalWeatherConditions.measurements.temp + "k", Toast.LENGTH_LONG);
t.show();
});
}
}
class ForecastTask implements Runnable {
private String city;
private Activity parentActivity;
public ForecastTask(Activity parent, String city) {
this.city = city;
parentActivity = parent;
}
@Override
public void run() {
Log.d("ForecastTask", "Getting forecast for " + city);
String url = "";
try {
url = "https://api.openweathermap.org/data/2.5/forecast?q=" + URLEncoder.encode(city, "UTF-8") + "&apiKey=830472d14b275cc781bbd5a0fe04e376";
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
WeatherForecast weatherForecast = null;
try {
InputStream response = new URL(url).openStream();
Reader reader = new InputStreamReader(response);
weatherForecast = new Gson().fromJson(reader, WeatherForecast.class);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
WeatherForecast finalWeatherForecast = weatherForecast;
parentActivity.runOnUiThread(() -> {
ListView listView = parentActivity.findViewById(R.id.listView);
assert finalWeatherForecast != null;
ArrayAdapter<WeatherForecastItem> arrayAdapter = new ArrayAdapter<>(parentActivity, android.R.layout.simple_list_item_1, finalWeatherForecast.list);
listView.setAdapter(arrayAdapter);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment