i'm nick i'm going to teach you how to make a weather up in this video you're 我是nick我要教你如何 在此视频中制作天气
going to learn how to get the user's location use it and also how to download 去学习如何获得用户的 位置使用它，以及如何下载
and parse through json data is so first things first we're gonna need to make a 和解析通过json数据是如此首先 事情首先我们需要做一个
new android studio project 新的Android工作室项目
call it like my weather app or something 称它像我的天气应用程序或东西
yeah
choose blank activity we're going to be using the floating action button so get 选择我们将要做的空白活动 使用浮动动作按钮这样得到
that in there 那里
right so here we are 所以这里我们是
we're going to need to do first is create a new Java class to download the
JSON data from a website good at java right click OK now get a job right but 我们需要做的第一个是 创建一个新的Java类来下载
get new Java class and call it download task and you're gonna need it to be in 获取新的Java类并调用它下载 任务，你会需要它在
the same packages the main activity so copy the package you stay here you're 同样打包主活动等等 复制你留在这里你的包
probably gonna get an error 获取新的Java类并调用它下载 任务，你会需要它在
just click move to the package and press ok and you'll be fine okay so now that 只需点击移动到包，然后按 确定，你会好的，所以现在
this is set up to download data from any web page you need to make it expect this 这是设置从任何下载数据 网页，你需要使它期望这一点
class extend async task so you do that like this 类扩展异步任务，所以你这样做 喜欢这个
alright and you still have a little area here you're going to click that and 好吧，你还有一个小区域 这里你要点击和
implement the methods doing background is we're going to download the webpage 实现方法做背景 是我们要下载的网页
data 数据
okay i'm going to call this URLs
now that you have that done we're going to go to the open weather map out our
website to get your API key set up
if you don't have an account go here and sign up and I don't generate an API key
for you if you do have an account you can sign in
right
we are there's my api key i'm going to be using for this tutorial and copy that
and put in another tab just so i have
you can reset this as many times as you want it doesn't matter so click on the
API to learn how to use it
one we're going to be using is the current weather data is this app is
going to get the weather data for the location where the person is currently
click on the api .
ok so when you're here we're going where the one we're going to be using is the
geographic coordinates will go here this for an example but it's invalid because
we don't have the right API kyun
your API key in there to get an example of how this is done this is what the
JSON data that gets returned looks like we're gonna have to do in the app is
download this and then parse through it to get certain things such as the name
of the city and where are you
Richard this is automatically in kelvin so eventually we're going to have to do
a little basic math to transfer that to fahrenheit or celsius depending upon
where you are
right so you got that copy this URL here
your API canít just put it somewhere where you can get it later because we're
going to be using that in the app
ok now we're going to start coding the download past class
go you make a couple global variables that we're going to need to use your
gonna make three you're gonna make the result variable which is a string
that's where the JSON data is going to be stored in the beginning when you
first download it
you're also going to need a URL
HTTP
section
alright so now we can start the
yeah
I'll leave that out so you can see within a minute why you need to have a
trying catch block there
you just create the new URL and you're going to create it from this what the
user inputs what we input
it's going to be from URLs 0
you have to surround this with trying cat
as this could fail for a number of reasons say the user doesn't have
internet connection or if nothing gets input here you have to put try and catch
around
and i'm just going to take the more faster route here and just put in a
general exception
we don't have to worry about any of that because it's not going to fail this is
just for learning purposes
and next you need to make a URL connection you use this URL connection
from the URL
asked to an HTTP URL connection
right now you're going to need to get an input stream from the your
httpurlconnection
need to have an inputstreamreader to read that input stream
what's being read from e inputstreamreader is going to be saved
in an integer called data
when it finishes reading it's going to end up being equal to negative 1 so
we're gonna need a while loop that runs wild data is not equal to negative one
that gets the data from here and saves it into the result string
and she got all that done
and you need to return the result because that's the string that the j
sunday is going to be saved him
cool need to go outside this method here and make a onpostexecute method
change this
result because that's the string we're going to be using
space invader ripe
you're going to make a new json object remember the JSON that's what I should
you right here all the code that follows is going to be to sift through this i'll
explain it line by line
alright you create a new json object from result result is going to be a
string that looks like this when you create a new json object is going to
detect the JSON and you'll be able to parse through it
so you've created a new json object for result we're gonna have to put try and
capture on this again because like i said before it could fail to use a
general exception again
ok
so after that you're going to make a new json object that has a more specific set
of data the weather data which will contain temperature stuff like that
he suited their json object that get string me the JSON object the first json
object is all this stuff right
Maine is everything that comes between this curly brace
and this curly brace so that gets the temperature pressure humidity
temperature men and venture max that good stuff that's really all we're going
to be working with now here we go
so then what you're going to need to do is get the temperature from mean now
it's stored as a string right now right here so we're gonna get it and parse it
to a double
like this
yeah
that stores it as a double but the problem with started as a double is that
when you displayed in the Attic that have a ton of decimal points decimal
numbers and the user is probably only going to want a whole number an integer
so we're going to change this to an integer will do that right now because
like i said before the temperatures and kelvin and we need to use a formula to
change it to Fahrenheit i'm using Fahrenheit because i'm in the u.s. I
don't wear it doesn't really matter where you are in the world you can use
the formula for Celsius but here's the Fahrenheit formula so degrees in Kelvin
x 1 point 8 minus that here we go
oh so that's all you have to do to change it
Fahrenheit
now you're probably going to also want to give these are the name of the place
in which they are so you do that like this show you where the places least
name is right here so you got to golike
so you have the necessary data now finish i'm going to show you how to
download it in the main activity
when your oncreate method make a new . make a new instance of the download ask
sorry I made a little mistake back in go to the download task class again and
these accidentally put them up here the first time you're going to need to take
them and put them in here and the doing background
and then you can go over to the main activity on create and create a new
instance of download task task
ok so easy to do over here to download basket accidentally got saved in the
test folder at it and paste it into your main folder
now when you try to create a new instance of download task
that down
then we're gonna have to do is call tasks . execute and give it a URL
the URL you're going to give it is this your
plus whatever year api key is
that
in here and you should be good
so that only works for one particular latitude and longitude though what we
want to do is to get the users latitude and longitude and use that in here i'm
going to use the built in android location nothing
she had to go just follow along with me
ok
going to get the last known location
you have to put the provider and the provider is the location provider on the
phone you can use a criteria like whatever the best provider is
really important right out for our purposes
so in android marshmallow
there's a new way of accepting permissions from the user or asking for
permissions you're going to do two things
access fine location to come that they're so NER so you got to do this and
it puts in this block of code i do basically handle what happens if the
user doesn't grant permission
we're not going to worry about that it just has to be there to get the users
latitude and longitude what you do is this is you have to create two variables
two doubles a latitude and longitude and get them from the location
so there you have your latitude and longitude down here in the URL you're
going to replace these numbers with your latitude and longitude with the
variables lat and alone
ok
alright so you have you URL you want to do now I mean you can log the data i
change the variables down here a bit just to let you know will be freaked out
it's still the same stuff
so what you need to do if you want to show the user the data is create a
couple text views if you're fine with just logging it up with the console to
prove yourself you know how to do that that's fine but I'm going to show you
guys out of show to the users
yeah
alright so i'm not going to worry about this right now I'm just gonna do it and
ximeno believe that going to paste a couple texts using here you guys can
pause the video copulas and if you want or just make your own text views
so then in your main activity you're going to create a variable containing
the texts and have to be static variables because you need to access
them from download past as well so called the first one place text you
ok so then here oncreate method you're going to give those of value
is he had those then in your download task method you're going to need to fill
each of these with the variables are with the values were getting from the
JSON data
yeah
I'll give me a second deleted the name so saying on this okay so i got the
place name they're gonna stick put in here
ok
one more thing if you want this to work you're gonna have to go to your
manifest.xml and put another permission for internet
alright that's it you rush you'll have a basic whether up hope you guys enjoyed
the video if you want me to continue making this even proving the design on
this app or if you want me to show you how to make any other types of apps let
me know in the comments section and subscribe to my channel thanks