Advertisement
Guest User

Untitled

a guest
Sep 30th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.82 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class NetworkService {
  7.     private const string xmlApi = "http://api.openweathermap.org/data/2.5/weather?id=701824&mode=xml&APPID=ed7e09a620553dba6828003117f600ab";
  8.  
  9.     private bool IsResponseValid(WWW www) {
  10.         if (www.error != null) {
  11.             Debug.Log ("bad connection");
  12.             return false;
  13.         } else if (string.IsNullOrEmpty (www.text)) {
  14.             Debug.Log ("bad data");
  15.             return false;
  16.         } else {
  17.             return true;
  18.         }
  19.     }
  20.  
  21.     private IEnumerator CallAPI(string url, Action<string> callback) {
  22.         WWW www = new WWW (url);
  23.         yield return www;
  24.  
  25.         if (!IsResponseValid (www)) {
  26.             yield break;
  27.         }
  28.         callback (www.text);
  29.     }
  30.  
  31.     public IEnumerator GetWeatherXML(Action<string> callback) {
  32.         return CallAPI (xmlApi, callback);
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement