[英]How to make a network call in Android without using an async call?
There is some gaps in android that I do not understand. 我不了解android中的一些差距。 There is a file I would like to make a network call in. I keep getting an error when I don't make a network call in either a activity or fragment.
我想打入一个文件。在活动或片段中都不打入网络时,我总是收到错误消息。
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;
}
}
EVEN Better if I can make the network call here: 即使可以在这里拨打网络电话,甚至更好:
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;
}
}
Simply, you cannot make network operations on UI thread. 简单来说,您无法在UI线程上进行网络操作。 Why would you like to do it anyway?
您为什么仍要这样做? Network call will block UI message loop - possibly for serveral seconds - this will quickly cause ANR error.
网络通话将阻止UI消息循环-可能持续数秒-这将迅速导致ANR错误。 There are various APIs that make network operations very easy, ie.
有多种API使网络操作变得非常容易。 OkHTTP .
OkHTTP 。
//It would be perfect if I can make the network call here
//如果可以在这里拨打网络电话,那将是完美的
then you will have to split this part in async operation, this will require restructuring your code - there is no way around. 那么您将不得不在异步操作中拆分这部分,这将需要重组代码-无法解决。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.