Android Question Electrical Flow Conditions Using Openweathermap’s Conditions Api

Posted on


OpenWeatherMap‘s weather condition API is simple, clear too free. This Android event present how access current weather condition information using OpenWeatherMap’s weather condition API; for whatsoever place on basis including over 200,000 cities! Current weather condition is oft updated based on global models too information from to a greater extent than than 40,000 weather condition stations. Data is available inward JSON, XML, or HTML format. This event role JSON.

To access the API you lot demand to sign upwards for an API key, this event role the demo API Key (appid inward the query)

MainActivity.java

package com.blogspot.android_er.androidopenweathermap;  import android.os.AsyncTask; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView;  import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject;  import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder;  world degree MainActivity extends AppCompatActivity {      EditText editTextCityName;     Button btnByCityName;     TextView textViewResult, textViewInfo;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         editTextCityName = (EditText)findViewById(R.id.cityname);         btnByCityName = (Button)findViewById(R.id.bycityname);         textViewResult = (TextView)findViewById(R.id.result);         textViewInfo = (TextView)findViewById(R.id.info);          btnByCityName.setOnClickListener(new View.OnClickListener() {             @Override             world void onClick(View v) {                 novel OpenWeatherMapTask(                         editTextCityName.getText().toString(),                         textViewResult).execute();             }         });     }      somebody degree OpenWeatherMapTask extends AsyncTask<Void, Void, String> {          String cityName;         TextView tvResult;          String dummyAppid = "bd82977b86bf27fb59a04b61b657fb6f";         String queryWeather = "http://api.openweathermap.org/data/2.5/weather?q=";         String queryDummyKey = "&appid=" + dummyAppid;          OpenWeatherMapTask(String cityName, TextView tvResult){             this.cityName = cityName;             this.tvResult = tvResult;         }          @Override         protected String doInBackground(Void... params) {             String effect = "";             String queryReturn;              String enquiry = null;             endeavor {                 enquiry = queryWeather + URLEncoder.encode(cityName, "UTF-8") + queryDummyKey;                 queryReturn = sendQuery(query);                 effect += ParseJSON(queryReturn);             } select care of (UnsupportedEncodingException e) {                 e.printStackTrace();                 queryReturn = e.getMessage();             } select care of (IOException e) {                 e.printStackTrace();                 queryReturn = e.getMessage();             }               terminal String finalQueryReturn = enquiry + "\n\n" + queryReturn;             runOnUiThread(new Runnable() {                 @Override                 world void run() {                     textViewInfo.setText(finalQueryReturn);                 }             });               provide result;         }          @Override         protected void onPostExecute(String s) {             tvResult.setText(s);         }          somebody String sendQuery(String query) throws IOException {             String effect = "";              URL searchURL = novel URL(query);              HttpURLConnection httpURLConnection = (HttpURLConnection)searchURL.openConnection();             if(httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK){                 InputStreamReader inputStreamReader = novel InputStreamReader(httpURLConnection.getInputStream());                 BufferedReader bufferedReader = novel BufferedReader(                         inputStreamReader,                         8192);                  String job = null;                 while((line = bufferedReader.readLine()) != null){                     effect += line;                 }                  bufferedReader.close();             }              provide result;         }          somebody String ParseJSON(String json){             String jsonResult = "";              endeavor {                 JSONObject JsonObject = novel JSONObject(json);                 String cod = jsonHelperGetString(JsonObject, "cod");                  if(cod != null){                     if(cod.equals("200")){                          jsonResult += jsonHelperGetString(JsonObject, "name") + "\n";                         JSONObject sys = jsonHelperGetJSONObject(JsonObject, "sys");                         if(sys != null){                             jsonResult += jsonHelperGetString(sys, "country") + "\n";                         }                         jsonResult += "\n";                          JSONObject coord = jsonHelperGetJSONObject(JsonObject, "coord");                         if(coord != null){                             String lon = jsonHelperGetString(coord, "lon");                             String lat = jsonHelperGetString(coord, "lat");                             jsonResult += "lon: " + lon + "\n";                             jsonResult += "lat: " + lat + "\n";                         }                         jsonResult += "\n";                          JSONArray weather condition = jsonHelperGetJSONArray(JsonObject, "weather");                         if(weather != null){                             for(int i=0; i<weather.length(); i++){                                 JSONObject thisWeather = weather.getJSONObject(i);                                 jsonResult += "weather " + i + ":\n";                                 jsonResult += "id: " + jsonHelperGetString(thisWeather, "id") + "\n";                                 jsonResult += jsonHelperGetString(thisWeather, "main") + "\n";                                 jsonResult += jsonHelperGetString(thisWeather, "description") + "\n";                                 jsonResult += "\n";                             }                         }                          JSONObject primary = jsonHelperGetJSONObject(JsonObject, "main");                         if(main != null){                             jsonResult += "temp: " + jsonHelperGetString(main, "temp") + "\n";                             jsonResult += "pressure: " + jsonHelperGetString(main, "pressure") + "\n";                             jsonResult += "humidity: " + jsonHelperGetString(main, "humidity") + "\n";                             jsonResult += "temp_min: " + jsonHelperGetString(main, "temp_min") + "\n";                             jsonResult += "temp_max: " + jsonHelperGetString(main, "temp_max") + "\n";                             jsonResult += "sea_level: " + jsonHelperGetString(main, "sea_level") + "\n";                             jsonResult += "grnd_level: " + jsonHelperGetString(main, "grnd_level") + "\n";                             jsonResult += "\n";                         }                          jsonResult += "visibility: " + jsonHelperGetString(JsonObject, "visibility") + "\n";                         jsonResult += "\n";                          JSONObject air current = jsonHelperGetJSONObject(JsonObject, "wind");                         if(wind != null){                             jsonResult += "wind:\n";                             jsonResult += "speed: " + jsonHelperGetString(wind, "speed") + "\n";                             jsonResult += "deg: " + jsonHelperGetString(wind, "deg") + "\n";                             jsonResult += "\n";                         }                          //...incompleted                      }else if(cod.equals("404")){                         String message = jsonHelperGetString(JsonObject, "message");                         jsonResult += "cod 404: " + message;                     }                 }else{                     jsonResult += "cod == null\n";                 }              } select care of (JSONException e) {                 e.printStackTrace();                 jsonResult += e.getMessage();             }              provide jsonResult;         }          somebody String jsonHelperGetString(JSONObject obj, String k){             String v = null;             endeavor {                 v = obj.getString(k);             } select care of (JSONException e) {                 e.printStackTrace();             }              provide v;         }          somebody JSONObject jsonHelperGetJSONObject(JSONObject obj, String k){             JSONObject o = null;              endeavor {                 o = obj.getJSONObject(k);             } select care of (JSONException e) {                 e.printStackTrace();             }              provide o;         }          somebody JSONArray jsonHelperGetJSONArray(JSONObject obj, String k){             JSONArray a = null;              endeavor {                 a = obj.getJSONArray(k);             } select care of (JSONException e) {                 e.printStackTrace();             }              provide a;         }     } }  

layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:padding="16dp"     android:orientation="vertical"     tools:context=".MainActivity">      <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center_horizontal"         android:autoLink="web"         android:text="https://rootandroid.org//"         android:textStyle="bold" />      <EditText         android:id="@+id/cityname"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:hint="City name"/>     <Button         android:id="@+id/bycityname"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="Call electrical flow weather condition past times metropolis name"/>     <LinearLayout         android:layout_width="match_parent"         android:layout_height="match_parent"         android:orientation="horizontal">         <ScrollView             android:layout_width="0dp"             android:layout_height="match_parent"             android:layout_weight="1">             <TextView                 android:id="@+id/result"                 android:layout_width="match_parent"                 android:layout_height="wrap_content"                 android:textSize="20dp"                 android:textStyle="bold"/>         </ScrollView>         <ScrollView             android:layout_width="0dp"             android:layout_height="match_parent"             android:layout_weight="1">             <TextView                 android:id="@+id/info"                 android:layout_width="match_parent"                 android:layout_height="wrap_content"                 android:textSize="18dp"                 android:textStyle="italic"/>         </ScrollView>     </LinearLayout>  </LinearLayout>  

Permission of “android.permission.INTERNET” is needed inward AndroidManifest.xml.

    <uses-permission android:name="android.permission.INTERNET"/> 

current weather condition information using OpenWeatherMap Android enquiry electrical flow weather condition using OpenWeatherMap's Weather APIDownload the files (Android Studio Format) .