Advertisement
Guest User

Untitled

a guest
Jan 17th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. package com.example.rafab.apptempo;
  2.  
  3. import android.os.AsyncTask;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7. import android.widget.ArrayAdapter;
  8. import android.widget.ListView;
  9.  
  10. import org.json.JSONObject;
  11.  
  12. import java.io.BufferedReader;
  13. import java.io.InputStreamReader;
  14. import java.net.HttpURLConnection;
  15. import java.net.URL;
  16. import java.util.ArrayList;
  17.  
  18.  
  19. public class MainActivity extends AppCompatActivity {
  20.  
  21. public JSONObject data = null;
  22. public ListView listView;
  23. public ArrayList<String> temp =new ArrayList<>();
  24.  
  25. @Override
  26. protected void onCreate(Bundle savedInstanceState) {
  27. super.onCreate(savedInstanceState);
  28. setContentView(R.layout.activity_main);
  29.  
  30. temp.add("Braga");
  31. temp.add("Porto");
  32. temp.add("Lisboa");
  33.  
  34. getJSON(temp);
  35.  
  36. listView = (ListView) findViewById(R.id.list);
  37. listView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,new ArrayList<String>()));
  38.  
  39. }
  40.  
  41. public void getJSON(final ArrayList<String> temperaturas) {
  42.  
  43.  
  44. new AsyncTask<Void, String, String>() {
  45. private ArrayAdapter<String> adapter;
  46. String data1;
  47.  
  48. @Override
  49. protected void onPreExecute() {
  50. //adapter=(ArrayAdapter<String>)listView.getAdapter();
  51. }
  52.  
  53. @Override
  54. protected String doInBackground(Void... params) {
  55.  
  56. for (String s : temperaturas){
  57. try {
  58.  
  59. URL url = new URL("https://api.openweathermap.org/data/2.5/weather?q=" + s + "&appid=27960ba27a09e2165642c128f0b66bda");
  60.  
  61. HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  62.  
  63. BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  64.  
  65. StringBuffer json = new StringBuffer(1024);
  66. String tmp = "";
  67.  
  68. while ((tmp = reader.readLine()) != null)
  69. json.append(tmp).append("\n");
  70. reader.close();
  71.  
  72. data = new JSONObject(json.toString());
  73.  
  74. if (data.getInt("cod") != 200) {
  75. System.out.println("Cancelled");
  76. return null;
  77. }
  78.  
  79. data1 = (String) data.toString();
  80.  
  81. } catch (Exception e) {
  82.  
  83. System.out.println("Exception " + e.getMessage());
  84. return null;
  85. }
  86.  
  87. //publishProgress(data1);
  88.  
  89. }
  90. return null;
  91. }
  92.  
  93. @Override
  94. protected void onPostExecute(String dataJson) {
  95. if (data != null) {
  96. Log.d("my weather received", data.toString());
  97.  
  98. }
  99.  
  100. }
  101. }.execute();
  102. }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement