Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.thoma.weather;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.view.inputmethod.InputMethod;
- import android.view.inputmethod.InputMethodManager;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.io.IOException;
- import java.net.URISyntaxException;
- import java.net.URL;
- import java.util.concurrent.Executor;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import cafsoft.foundation.Data;
- public class MainActivity extends AppCompatActivity {
- private EditText editText = null;
- private EditText editText2 = null;
- private Button button = null;
- private TextView textView7 = null;
- private TextView textView8 = null;
- private TextView textView9 = null;
- private ExecutorService queue = null;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- editText = findViewById(R.id.editText);
- editText2 = findViewById(R.id.editText2);
- button = findViewById(R.id.button);
- textView7 = findViewById(R.id.textView7);
- textView8 = findViewById(R.id.textView8);
- textView9 = findViewById(R.id.textView9);
- button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- getInfo();
- }
- });
- if (queue == null){
- queue = Executors.newSingleThreadExecutor();
- }
- }
- public void getInfo(){
- final String countryISOCode = editText.getText().toString();
- final String cityName = editText2.getText().toString();
- final String urlWebService = "http://api.openweathermap.org/data/2.5/weather/";
- final String query = "q=" + cityName + "," + countryISOCode;
- final String appIDParam = "APPID=122a0522194923306c771c2f6a258c09";
- final String unitsParam = "units=metric";
- final String langParam = "lang=es";
- final String params = query + "&" + appIDParam + "&" + unitsParam + "&" + langParam;
- final String strURL = urlWebService + "?" + params;
- final InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
- imm.hideSoftInputFromWindow(editText2.getWindowToken(), 0 );
- final Runnable r = new Runnable(){
- @Override
- public void run() {
- Data data = null;
- URL url = null;
- Log.d("Weather",strURL);
- try {
- url = new URL(strURL);
- data = new Data(url);
- }catch (IOException e){
- e.printStackTrace();
- }catch (URISyntaxException e){
- e.printStackTrace();
- }
- if (data != null){
- JSONObject info = null;
- try{
- info = new JSONObject(data.toText());
- }catch (JSONException e){
- e.printStackTrace();
- }
- if (info != null){
- float t = 0;
- float tMin = 0;
- float tMax = 0;
- JSONObject main = null;
- try {
- main = info.getJSONObject("main");
- t = (float)main.getDouble( "temp");
- tMin = (float)main.getDouble( "temp_min");
- tMax = (float)main.getDouble( "temp_max");
- }catch (JSONException e){
- e.printStackTrace();
- }
- final float temp = t;
- final float tempMin = tMin;
- final float tempMax = tMax;
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- textView7.setText(String.valueOf(temp));
- textView8.setText(String.valueOf(tempMin));
- textView9.setText(String.valueOf(tempMax));
- }
- });
- }
- }
- }
- };
- textView7.setText("");
- textView8.setText("");
- textView9.setText("");
- queue.execute(r);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement