我使用以下代码调用API ...
var map = L.map('map', {
center: [51.501617,-0.018582],
zoom: 14
});
L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}', {
maxZoom: 18,
id: 'jeffceriello.nd7obhnh',
accessToken: 'pk.eyJ1IjoiamVmZmNlcmllbGxvIiwiYSI6Ikhrakxrd00ifQ.SlVngzIXeS5UPC8UGmy1OA'
}).addTo(map);
map.scrollWheelZoom.disable();
// marker
var churchill = L.icon({
iconUrl: '/img/pin.png',
shadowUrl: '',
iconSize: [43, 64], // size of the icon
shadowSize: [0, 0], // size of the shadow
iconAnchor: [21, 64], // point of the icon which will correspond to marker's location
shadowAnchor: [0, 0], // the same for the shadow
popupAnchor: [0, 0] // point from which the popup should open relative to the iconAnchor
});
L.marker([51.503754,-0.014841], {icon: churchill}).addTo(map);
var geojson = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [-0.014782, 51.504711]
},
"properties": {
"title": "Barclays Bank",
markerColor: "#6D8AAA"
}
},{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [-0.014618, 51.504648]
},
"properties": {
"title": "Idea Store",
markerColor: "#6D8AAA"
}
},{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [-0.014532, 51.504556]
},
"properties": {
"title": "James Shoe Care",
markerColor: "#6D8AAA"
}
}
]};
shopping = L.geoJson(geojson, {
style: function(feature) {
return {color: feature.properties.markerColor};
},
pointToLayer: function(feature, latlng) {
return new L.CircleMarker(latlng, {radius: 8, fillOpacity: 1, stroke: false});
},
onEachFeature: function (feature, layer) {
layer.bindPopup(feature.properties.title);
}
});
map.addLayer(shopping);
var geojson2 = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [-0.014546, 51.504319]
},
"properties": {
"title": "Birleys",
"markerColor": "#6D8AAA"
}
}
]};
food = L.geoJson(geojson2, {
style: function(feature) {
return {color: feature.properties.markerColor};
},
pointToLayer: function(feature, latlng) {
return new L.CircleMarker(latlng, {radius: 8, fillOpacity: 1, stroke: false});
},
onEachFeature: function (feature, layer) {
layer.bindPopup(feature.properties.title);
}
});
//map.addLayer(food);
var overlayMaps = {
"Shopping": shopping,
"Food & Drink": food
};
var control = L.control.layers(overlayMaps, null, {collapsed: false});
control.addTo(map);
control._container.remove();
document.getElementById('layers').appendChild(control.onAdd(map));
以上是:
public async Task<Dictionary<string, string>> GetDataAsync()
{
try
{
var url = "https://address.com/myapi";
var request = new RestRequest(url, Method.GET) { Timeout = 5000 };
request.AddAuthenticationHeaders();
var response = await _client.ExecuteTaskAsync(request);
return ProcessResponse(response);
}
catch (Exception e)
{
throw new MyApplicationException(e.Message, e.InnerException, AlertCode.UnknownError);
}
}
此代码处理所有错误并显示正确的弹出窗口和应用程序继续但仅在此特定错误上应用程序崩溃
public async Task GetData()
{
data= await webCaller.GetDataAsync();
}
我通过调用API并关闭连接来重现此错误,因此很明显网络无法访问。
在我的活动中,我在这里处理异常
System.Net.WebException: Error: ConnectFailure (Network is unreachable) ---> System.Net.Sockets.SocketException: Network is unreachable
OnResume:
protected virtual void OnException_Occured(object sender, RaiseThrowableEventArgs e)
{
RunOnUiThread(
() =>
{
ExceptionPopup.Popup = new CommonPopup(this, ExceptionHelper.GetUserFriendlyMessage(e.Exception));
ExceptionPopup.Popup.Show();
});
}
ReportError(e.Exception);
e.Handled = true;
}
OnPause:
protected override void OnResume()
{
base.OnResume();
AndroidEnvironment.UnhandledExceptionRaiser += OnException_Occured;
}
答案 0 :(得分:1)
每次拨打GetData()
时它会崩溃,还是随机崩溃?
今天我遇到了类似的问题,我的异步API调用使iOS应用程序随机崩溃。我不确定我的问题是否与您的问题相关,但我会解释我所做的事情以防万一。
在我的情况下,问题结果与连接没有任何关系。由于某种原因,API调用并不总是成功,当它失败时,它会向我的变量返回null,这会导致“对象未设置为对象的实例”错误。由于我无法保证每次我的API调用都返回非null值,我所做的就是将API调用放在while循环中并检查我是否得到null。
这是我的代码示例:
Message[] allMessages;
while(allMessages==null)
{
allMessages = await Message.GetAsync();
}