나는 안드로이드와 윈도우 폰용 날씨 애플리케이션에서 일하고있다. 그래서 "Parsing Class"의 "Weather.Api (Portable)"에 다른 프로젝트에서 공통 코드를 작성합니다. "Weather.Droid"는 Android에 사용됩니다.
ParsingClass의 코드는 다음과 같습니다.
public static string tempGlobal;
public static string cityTextGlobal;
private static string GovnoTemperature;
public async Task<string> dataByCity(string city)
{
var url = "http://api.openweathermap.org/data/2.5/weather?q=" + city + "&units=metric&APPID="+AppID;
//ТОРМОЗИТ ЗДЕСЬ / BRAKES HERE
await FetchAsync(url);
return city;
}
public async Task<double> Data_down(double lat, double lon)
{
var url = String.Format(
"http://api.openweathermap.org/data/2.5/weather?lat=" + lat + "&lon=" + lon + "&units=metric&APPID=" + AppID);
//ТОРМОЗИТ ЗДЕСЬ / BRAKES HERE
await FetchAsync(url);
return lat;
}
public async Task<string> FetchAsync(string url)
{
string jsonString;
using (var httpClient = new System.Net.Http.HttpClient())
{
var stream = await httpClient.GetStreamAsync(url);
StreamReader reader = new StreamReader(stream);
jsonString = reader.ReadToEnd();
}
var json = jsonString;
JsonValue firstitem = json;
var mydata = JObject.Parse(json);
cityTextGlobal = (mydata["name"]).ToString();
string GovnoData = (mydata["main"]).ToString();
//spliting string
string[] values = GovnoData.Split(',');
for (int i = 0; i < values.Length; i++)
{
values[i] = values[i].Trim();
if (i == 0)
{
//tempGlobal = values[i];
GovnoTemperature = values[i];
}
}
tempGlobal = null;
foreach (char c in GovnoTemperature)
{
if (c == '.')
{
break;
}
if (c == '-' || char.IsDigit(c) == true || c == '.')
{
tempGlobal += c.ToString();
}
}
return jsonString;
}
}
public class Coord
{
public double lon { get; set; }
public double lat { get; set; }
}
public class Weather
{
public int id { get; set; }
public string main { get; set; }
public string description { get; set; }
public string icon { get; set; }
}
public class Main
{
public double temp { get; set; }
public int pressure { get; set; }
public int humidity { get; set; }
public int temp_min { get; set; }
public int temp_max { get; set; }
}
public class Wind
{
public double speed { get; set; }
public double deg { get; set; }
}
public class Clouds
{
public int all { get; set; }
}
public class Sys
{
public int type { get; set; }
public int id { get; set; }
public double message { get; set; }
public string country { get; set; }
public int sunrise { get; set; }
public int sunset { get; set; }
}
public class RootObject
{
public Coord coord { get; set; }
public List<Weather> weather { get; set; }
public string @base { get; set; }
public Main main { get; set; }
public int visibility { get; set; }
public Wind wind { get; set; }
public Clouds clouds { get; set; }
public int dt { get; set; }
public Sys sys { get; set; }
public int id { get; set; }
public string name { get; set; }
public int cod { get; set; }
}
}
메서드에서 "Weather.Droid"의 MainActivity를 시작해야합니다.
공용 비동기 작업 FetchAsync (string url)
반환하기 전에 :
jsonString
어떻게 할 수 있습니까?
메서드에서 "Weather.Droid"의 MainActivity를 시작해야합니다.
공용 비동기 작업 FetchAsync (string url)
Droid 프로젝트에서 직접 MainActivity를 시작할 수 없습니다. 그러나 @SKall이 말했듯이 방법 Action<Intent>
에서 사용할 수 있습니다 FetchAsync
.
//add Action<Intent> as and Intent as parameters
public async Task<string> FetchAsync(string url,Action<Intent> navigationFunc,Intent intent)
{
string jsonString;
...
navigationFunc(intent);//Invoke the action
return jsonString;
}
그런 다음 Droid 프로젝트에서 사용하십시오.
ParsingClass parsing = new ParsingClass();
Intent intent = new Intent(this, typeof(MainActivity));
await parsing.FetchAsync("url", StartActivity, intent);
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다