[英]How to make a network call in Android without using an async call?
我不了解android中的一些差距。 我想打入一個文件。在活動或片段中都不打入網絡時,我總是收到錯誤消息。
public class Attraction {
public String name;
public String description;
public String longDescription;
public Uri imageUrl;
public Uri secondaryImageUrl;
public LatLng location;
public String city;
public Bitmap image;
public Bitmap secondaryImage;
public String distance;
public Attraction() {}
public Attraction(String name, String description, String longDescription, Uri imageUrl,
Uri secondaryImageUrl, LatLng location, String city) {
//I am looking to replace these variables with information from a website
this.name = "Hey";//name;
this.description = description;
this.longDescription = longDescription;
this.imageUrl = imageUrl;
this.secondaryImageUrl = secondaryImageUrl;
this.location = location;
this.city = city;
}
}
即使可以在這里撥打網絡電話,甚至更好:
public class TouristAttractions extends Main2Activity{
public static final String CITY_SYDNEY = "Sydney";
public static final String TEST_CITY = CITY_SYDNEY;
private static final float TRIGGER_RADIUS = 2000; // 2KM
private static final int TRIGGER_TRANSITION = Geofence.GEOFENCE_TRANSITION_ENTER |
Geofence.GEOFENCE_TRANSITION_EXIT;
private static final long EXPIRATION_DURATION = Geofence.NEVER_EXPIRE;
public static final Map<String, LatLng> CITY_LOCATIONS = new HashMap<String, LatLng>() {{
put(CITY_SYDNEY, new LatLng(-33.873651, 151.2068896));
}};
/**
* All photos used with permission under the Creative Commons Attribution-ShareAlike License.
*/
public static final HashMap<String, List<Attraction>> ATTRACTIONS =
new HashMap<String, List<Attraction>>() {{
//It would be perfect if I can make the network call here
put(CITY_SYDNEY, new ArrayList<Attraction>() {{
add(new Attraction(
"France",
"Lovely place",
"You should go",
Uri.parse("http://....png"),
Uri.parse("http://.....png"),
new LatLng(-33.858667, 151.214028),
CITY_SYDNEY
));
}});
}};
/**
* Creates a list of geofences based on the city locations
*/
public static List<Geofence> getGeofenceList() {
List<Geofence> geofenceList = new ArrayList<Geofence>();
for (String city : CITY_LOCATIONS.keySet()) {
LatLng cityLatLng = CITY_LOCATIONS.get(city);
geofenceList.add(new Geofence.Builder()
.setCircularRegion(cityLatLng.latitude, cityLatLng.longitude, TRIGGER_RADIUS)
.setRequestId(city)
.setTransitionTypes(TRIGGER_TRANSITION)
.setExpirationDuration(EXPIRATION_DURATION)
.build());
}
return geofenceList;
}
public static String getClosestCity(LatLng curLatLng) {
if (curLatLng == null) {
// If location is unknown return test city so some data is shown
return TEST_CITY;
}
double minDistance = 0;
String closestCity = null;
for (Map.Entry<String, LatLng> entry: CITY_LOCATIONS.entrySet()) {
double distance = SphericalUtil.computeDistanceBetween(curLatLng, entry.getValue());
if (minDistance == 0 || distance < minDistance) {
minDistance = distance;
closestCity = entry.getKey();
}
}
return closestCity;
}
}
簡單來說,您無法在UI線程上進行網絡操作。 您為什么仍要這樣做? 網絡通話將阻止UI消息循環-可能持續數秒-這將迅速導致ANR錯誤。 有多種API使網絡操作變得非常容易。 OkHTTP 。
//如果可以在這里撥打網絡電話,那將是完美的
那么您將不得不在異步操作中拆分這部分,這將需要重組代碼-無法解決。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.