Share Lập Trình Ứng Dụng Thời Tiết Trực Tuyến Trong VB.NET: Sử Dụng Weather APIs

blackbird329

New member
#vb.net #Weather #API #WebApp #Programming ### Lập trình ứng dụng thời tiết trực tuyến trong VB.NET: Sử dụng API thời tiết

**Giới thiệu**

Trong hướng dẫn này, chúng tôi sẽ tìm hiểu cách tạo một ứng dụng thời tiết trực tuyến đơn giản trong VB.NET bằng cách sử dụng [API OpenWeathermap] (https://openweathermap.org/api).Ứng dụng sẽ cho phép người dùng nhập tên thành phố và có được các điều kiện thời tiết hiện tại, bao gồm nhiệt độ, độ ẩm, tốc độ gió và lượng mưa.

** Điều kiện tiên quyết **

Để làm theo với hướng dẫn này, bạn sẽ cần những điều sau đây:

* Một máy tính với Visual Studio được cài đặt
* Khóa API miễn phí từ [OpenWeathermap] (Weather API - OpenWeatherMap)

** Tạo dự án **

Đầu tiên, chúng ta cần tạo một dự án mới trong Visual Studio.Để làm điều này, hãy mở Visual Studio và chọn tệp **> mới> Dự án **.Trong hộp thoại ** New Project **, chọn ** Visual Basic> Windows> Windows Forms Ứng dụng ** và nhấp vào ** OK **.

Điều này sẽ tạo ra một dự án mới có tên ** WeatherApp **.Trong ** Giải pháp Explorer **, bạn sẽ thấy một thư mục có tên ** form1.vb **.Đây là hình thức chính cho ứng dụng của chúng tôi.

** Thêm API OpenWeathermap **

Bước tiếp theo là thêm API OpenWeathermap vào dự án của chúng tôi.Để thực hiện việc này, nhấp chuột phải vào nút ** tham chiếu ** nút trong ** Solution Explorer ** và chọn ** Thêm tham chiếu **.Trong hộp thoại ** Thêm tham chiếu **, chọn tab **.Chọn lắp ráp và nhấp vào ** OK **.

** Nhận các điều kiện thời tiết hiện tại **

Bây giờ chúng tôi có API Openweathermap được thêm vào dự án của chúng tôi, chúng tôi có thể bắt đầu nhận được các điều kiện thời tiết hiện tại.Để làm điều này, chúng ta cần tạo một thể hiện mới của lớp ** httpclient **.Lớp ** httpclient ** được sử dụng để gửi các yêu cầu HTTP đến máy chủ web.

`` `
Dim Client dưới dạng httpclient mới ()
`` `

Khi chúng tôi đã tạo một thể hiện của lớp ** httpclient **, chúng tôi có thể sử dụng nó để gửi yêu cầu đến API OpenWeathermap.Mã sau đây cho thấy cách có được điều kiện thời tiết hiện tại cho một thành phố:

`` `
Yêu cầu Dim dưới dạng httprequestMessage mới ()
request.method = httpmethod.get
request.Requesturi = new URI ("https://api.openweathermap.org/data/2.5/weather?q= tên

Phản hồi DIM là httpresponsemessage = client.sendasync (yêu cầu) .Result

Dim json dưới dạng chuỗi = answer.content.ReadAssTringAsync (). Kết quả kết quả.

Thời tiết mờ như thời tiết = jsonconvert.deserializeObject (JSON)
`` `

Dòng mã đầu tiên tạo ra một thể hiện mới của lớp ** httprequestMessage **.Lớp ** httprequestMessage ** được sử dụng để thể hiện yêu cầu HTTP.

Dòng mã thứ hai đặt thuộc tính ** Phương thức ** của ** httprequestMessage ** đối tượng thành ** httpmethod.get **.Điều này cho thấy rằng chúng tôi đang gửi yêu cầu nhận đến máy chủ.

Dòng mã thứ ba đặt thuộc tính ** requesturi ** của đối tượng ** httprequestMessage ** vào một URL trỏ đến API OpenWeathermap.URL bao gồm tên thành phố và khóa API.

Dòng mã thứ tư gửi yêu cầu đến máy chủ bằng phương thức ** sendasync ** của lớp ** httpclient **.Phương thức ** sendasync ** trả về một đối tượng ** ** đối tượng đại diện cho hoạt động không đồng bộ của việc gửi yêu cầu.

Dòng mã thứ năm nhận được phản hồi từ máy chủ bằng cách sử dụng thuộc tính ** kết quả ** của đối tượng ** tác vụ **.Thuộc tính ** kết quả ** trả về đối tượng ** httpresponemessage ** chứa phản hồi từ máy chủ.

Dòng mã thứ sáu đọc phần thân phản hồi dưới dạng chuỗi bằng phương thức ** readasstringasync ** của đối tượng ** httpresponemessage **.

Dòng mã thứ bảy giảm dần phần thân phản hồi JSON thành một đối tượng ** thời tiết ** bằng cách sử dụng phương thức ** jsonconvert.deserializeObject **.

Đối tượng ** thời tiết ** chứa các điều kiện thời tiết hiện tại cho thành phố.Sau đây là các thuộc tính của đối tượng ** thời tiết **
=======================================
#vb.net #Weather #API #WebApp #Programming ### Online weather application programming in VB.NET: Use Weather APIs

**Introduction**

In this tutorial, we will learn how to create a simple online weather application in VB.NET using the [OpenWeatherMap API](https://openweathermap.org/api). The application will allow users to enter a city name and get the current weather conditions, including the temperature, humidity, wind speed, and precipitation.

**Prerequisites**

To follow along with this tutorial, you will need the following:

* A computer with Visual Studio installed
* A free API key from [OpenWeatherMap](https://openweathermap.org/api)

**Creating the Project**

First, we need to create a new project in Visual Studio. To do this, open Visual Studio and select **File > New > Project**. In the **New Project** dialog box, select **Visual Basic > Windows > Windows Forms Application** and click **OK**.

This will create a new project called **WeatherApp**. In the **Solution Explorer**, you will see a folder called **Form1.vb**. This is the main form for our application.

**Adding the OpenWeatherMap API**

The next step is to add the OpenWeatherMap API to our project. To do this, right-click on the **References** node in the **Solution Explorer** and select **Add Reference**. In the **Add Reference** dialog box, select the **.NET Framework** tab and scroll down until you find the **System.Net.Http** assembly. Select the assembly and click **OK**.

**Getting the Current Weather Conditions**

Now that we have the OpenWeatherMap API added to our project, we can start getting the current weather conditions. To do this, we need to create a new instance of the **HttpClient** class. The **HttpClient** class is used to send HTTP requests to a web server.

```
Dim client As New HttpClient()
```

Once we have created an instance of the **HttpClient** class, we can use it to send a request to the OpenWeatherMap API. The following code shows how to get the current weather conditions for a city:

```
Dim request As New HttpRequestMessage()
request.Method = HttpMethod.Get
request.RequestUri = New Uri("https://api.openweathermap.org/data/2.5/weather?q={city name}&units=imperial&appid={API key}")

Dim response As HttpResponseMessage = client.SendAsync(request).Result

Dim json As String = response.Content.ReadAsStringAsync().Result

Dim weather As Weather = JsonConvert.DeserializeObject(json)
```

The first line of code creates a new instance of the **HttpRequestMessage** class. The **HttpRequestMessage** class is used to represent an HTTP request.

The second line of code sets the **Method** property of the **HttpRequestMessage** object to **HttpMethod.Get**. This indicates that we are sending a GET request to the server.

The third line of code sets the **RequestUri** property of the **HttpRequestMessage** object to a URL that points to the OpenWeatherMap API. The URL includes the city name and the API key.

The fourth line of code sends the request to the server using the **SendAsync** method of the **HttpClient** class. The **SendAsync** method returns a **Task** object that represents the asynchronous operation of sending the request.

The fifth line of code gets the response from the server using the **Result** property of the **Task** object. The **Result** property returns the **HttpResponseMessage** object that contains the response from the server.

The sixth line of code reads the response body as a string using the **ReadAsStringAsync** method of the **HttpResponseMessage** object.

The seventh line of code deserializes the JSON response body into a **Weather** object using the **JsonConvert.DeserializeObject** method.

The **Weather** object contains the current weather conditions for the city. The following are the properties of the **Weather** object
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top