Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2018
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.76 KB | None | 0 0
  1. package com.example.thoma.weather;
  2.  
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.util.Log;
  6. import android.view.View;
  7. import android.view.inputmethod.InputMethod;
  8. import android.view.inputmethod.InputMethodManager;
  9. import android.widget.Button;
  10. import android.widget.EditText;
  11. import android.widget.TextView;
  12.  
  13. import org.json.JSONException;
  14. import org.json.JSONObject;
  15.  
  16. import java.io.IOException;
  17. import java.net.URISyntaxException;
  18. import java.net.URL;
  19. import java.util.concurrent.Executor;
  20. import java.util.concurrent.ExecutorService;
  21. import java.util.concurrent.Executors;
  22.  
  23. import cafsoft.foundation.Data;
  24.  
  25. public class MainActivity extends AppCompatActivity {
  26.  
  27.     private EditText editText = null;
  28.     private EditText editText2 = null;
  29.     private Button button = null;
  30.     private TextView textView7 = null;
  31.     private TextView textView8 = null;
  32.     private TextView textView9 = null;
  33.  
  34.     private ExecutorService queue = null;
  35.  
  36.     @Override
  37.     protected void onCreate(Bundle savedInstanceState) {
  38.         super.onCreate(savedInstanceState);
  39.         setContentView(R.layout.activity_main);
  40.  
  41.         editText = findViewById(R.id.editText);
  42.         editText2 = findViewById(R.id.editText2);
  43.         button = findViewById(R.id.button);
  44.         textView7 = findViewById(R.id.textView7);
  45.         textView8 = findViewById(R.id.textView8);
  46.         textView9 = findViewById(R.id.textView9);
  47.  
  48.  
  49.         button.setOnClickListener(new View.OnClickListener() {
  50.             @Override
  51.             public void onClick(View v) {
  52.  
  53.                 getInfo();
  54.             }
  55.  
  56.         });
  57.         if (queue == null){
  58.             queue = Executors.newSingleThreadExecutor();
  59.         }
  60.     }
  61.     public void getInfo(){
  62.         final String countryISOCode = editText.getText().toString();
  63.         final String cityName = editText2.getText().toString();
  64.         final String urlWebService = "http://api.openweathermap.org/data/2.5/weather/";
  65.  
  66.         final String query = "q=" + cityName + "," + countryISOCode;
  67.         final String appIDParam = "APPID=122a0522194923306c771c2f6a258c09";
  68.         final String unitsParam = "units=metric";
  69.         final String langParam = "lang=es";
  70.  
  71.         final String params = query + "&" + appIDParam + "&" + unitsParam + "&" + langParam;
  72.  
  73.         final String strURL = urlWebService + "?" + params;
  74.  
  75.         final InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
  76.         imm.hideSoftInputFromWindow(editText2.getWindowToken(), 0 );
  77.  
  78.         final Runnable r = new Runnable(){
  79.  
  80.             @Override
  81.             public void run() {
  82.                 Data data = null;
  83.                 URL url = null;
  84.  
  85.                 Log.d("Weather",strURL);
  86.                 try {
  87.                     url = new URL(strURL);
  88.                     data = new Data(url);
  89.                 }catch (IOException e){
  90.                     e.printStackTrace();
  91.                 }catch (URISyntaxException e){
  92.                     e.printStackTrace();
  93.                 }
  94.  
  95.  
  96.                 if (data != null){
  97.                     JSONObject info = null;
  98.                     try{
  99.                         info = new JSONObject(data.toText());
  100.                     }catch (JSONException e){
  101.                         e.printStackTrace();
  102.                     }
  103.                     if (info != null){
  104.                         float t = 0;
  105.                         float tMin = 0;
  106.                         float tMax = 0;
  107.  
  108.                         JSONObject main = null;
  109.  
  110.                         try {
  111.                             main = info.getJSONObject("main");
  112.                             t = (float)main.getDouble( "temp");
  113.                             tMin = (float)main.getDouble(  "temp_min");
  114.                             tMax = (float)main.getDouble(  "temp_max");
  115.  
  116.  
  117.                         }catch (JSONException e){
  118.                             e.printStackTrace();
  119.                         }
  120.                         final float temp = t;
  121.                         final float tempMin = tMin;
  122.                         final float tempMax = tMax;
  123.  
  124.                         runOnUiThread(new Runnable() {
  125.                             @Override
  126.                             public void run() {
  127.  
  128.                                 textView7.setText(String.valueOf(temp));
  129.                                 textView8.setText(String.valueOf(tempMin));
  130.                                 textView9.setText(String.valueOf(tempMax));
  131.  
  132.                             }
  133.                         });
  134.                     }
  135.                 }
  136.             }
  137.         };
  138.  
  139.         textView7.setText("");
  140.         textView8.setText("");
  141.         textView9.setText("");
  142.  
  143.         queue.execute(r);
  144.  
  145.  
  146.     }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement