繁体   English   中英

如何在不使用异步通话的情况下在Android中进行网络通话?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM