如何在不使用异步调用的情况下在Android中进行网络呼叫?

时间:2015-12-13 23:34:06

标签: java android

我不明白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;
    }


}

1 个答案:

答案 0 :(得分:3)

简单地说,您无法在UI线程上进行网络操作。你为什么要这样做?网络调用将阻止UI消息循环 - 可能持续几秒钟 - 这将很快导致ANR错误。有各种API使网络操作非常容易,即。 OkHTTP

  

//如果我可以在这里进行网络通话,那将是完美的

那么你将不得不在异步操作中拆分这部分,这将需要重构你的代码 - 没有办法解决。