Demo
0. api 등록
https://openweathermap.org/api
1. 가입
2. my api key에서 key 복사
3. 맘에 드는 api 쓰면 된다.
1. 구성
- dependency
- permission
- network-security-config
- activity
- layout
Dependency 추가
implementation 'com.android.volley:volley:1.1.1'
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.recyclerview">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:networkSecurityConfig="@xml/network_security_config"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.RecyclerView">
<activity android:name=".MainActivity"
android:theme="@style/Theme.AppCompat.NoActionBar"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java
package com.example.recyclerview;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.text.DecimalFormat;
public class MainActivity extends AppCompatActivity {
private EditText etCity, etCountry;
private TextView tvResult;
private final String url = "http://api.openweathermap.org/data/2.5/weather";
private final String appid = "ea33671fc852a581e32410d45bed8052";
private DecimalFormat df = new DecimalFormat("#.##");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etCity = findViewById(R.id.et_city);
etCountry = findViewById(R.id.et_country);
tvResult = findViewById(R.id.tv_result);
}
public void getWeatherDetails(View view) {
String tempUrl = "";
String city = etCity.getText().toString().trim();
String country = etCountry.getText().toString().trim();
if(city.equals("")) {
tvResult.setText("City field can not be empty!");
} else {
if(!country.equals("")){
tempUrl = url + "?q=" + city + "," + country + "&appid=" +appid;
} else {
tempUrl = url + "?q=" + city + "&appid=" +appid;
}
StringRequest stringRequest = new StringRequest(Request.Method.POST, tempUrl, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("response>>>>>>>", response);
//json data 하나하나 꺼내는건데 이게 의미가 있는지 모르겟다
String output = "";
try {
JSONObject jsonResponse = new JSONObject(response);
JSONArray jsonArray = jsonResponse.getJSONArray("weather");
JSONObject jsonObjectWeather = jsonArray.getJSONObject(0);
String description = jsonObjectWeather.getString("description");
JSONObject jsonObjectMain = jsonResponse.getJSONObject("main");
double temp = jsonObjectMain.getDouble("temp") - 273.15;
double feelsLike = jsonObjectMain.getDouble("feels_like") - 273.15;
float pressure = jsonObjectMain.getInt("pressure");
int humidity = jsonObjectMain.getInt("humidity");
JSONObject jsonObjectWind = jsonResponse.getJSONObject("wind");
String wind = jsonObjectWind.getString("speed");
JSONObject jsonObjectCloud = jsonResponse.getJSONObject("clouds");
String clouds = jsonObjectCloud.getString("all");
JSONObject jsonObjectSys = jsonResponse.getJSONObject("sys");
String countryName = jsonObjectSys.getString("country");
String cityName = jsonResponse.getString("name");
output += "Current weather of " + cityName + " (" + countryName + ")"
+ "\n Temp: " + df.format(temp) + " 도"
+ "\n Feels LIke: " + df.format(feelsLike) + " 도"
+ "\n Humidity: " + humidity + "%"
+ "\n Description: " + description
+ "\n Wind Speed: " + wind + "m/s (meters per second)"
+ "\n Cloudiness: " + clouds + "%"
+ "\n Pressure: " + pressure + "hPa";
tvResult.setText(output);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//Log.d("error>>>>>>>", error.toString());
}
});
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
requestQueue.add(stringRequest);
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="16dp">
<TextView
android:id="@+id/text_view_01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Weather Update"
android:layout_gravity="center"
android:textSize="32dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_city"
android:layout_marginBottom="10dp"
android:hint="Enter City Name"
android:inputType="textPersonName"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_country"
android:layout_marginBottom="10dp"
android:hint="Enter Country Code"
android:inputType="textPersonName"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_get"
android:layout_marginBottom="10dp"
android:onClick="getWeatherDetails"
android:text="Get"
android:backgroundTint="@null"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="150dp">
<TextView
android:id="@+id/tv_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</ScrollView>
</LinearLayout>
</LinearLayout>
/res/xml/network_security_config.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true"/>
</network-security-config>
'System Programming > Android' 카테고리의 다른 글
[Android/Java] 터치하면 위로올라가는 게임 (0) | 2021.06.14 |
---|---|
[Android/Java] video view 가로 모드 (0) | 2021.06.14 |
[Android/Java] 타이머 (0) | 2021.06.14 |
[Android/Java] File stream 외부 Storage에 글 읽고 쓰기 (0) | 2021.06.14 |
[Android/Java] Text To Speech (0) | 2021.06.14 |