Http Url connection to fetch data android

306 Views Asked by At

It is giving an error

Error:(57, 16) error: cannot find symbol class HttpResponseException

I have implemented this code but most of the libraries are deprecated and cannot find any solution.

Is any updated code available to fetch the details of the place using google api.

The code is given below:

public class GooglePlaces
{
private static final HttpTransport HTTP_TRANSPORT=new NetHttpTransport();

private static final String API_KEY="Myapikey";

private static final String PLACES_SEARCH_URL="https://maps.googleapis.com/maps/api/place/search/json?";
private static final String PLACES_TEXT_SEARCH_URL="https://maps.googleapis.com/maps/api/place/search/json?";
private static final String PLACES_DETAILS_URL="https://maps.googleapis.com/maps/api/place/details/json?";

private double _latitude;
private double _longitude;
private double _radius;

public PlacesList search(double latitude, double longitude,double radius, String types) throws Exception
{
    this._latitude=latitude;
    this._longitude=longitude;
    this._radius=radius;

    try
    {
        HttpRequestFactory httpRequestFactory  =createRequestFactory(HTTP_TRANSPORT);
        HttpRequest request=httpRequestFactory
                .buildGetRequest(new GenericUrl (PLACES_SEARCH_URL));
        request.getUrl().put("key",API_KEY);
        request.getUrl().put("location",_latitude+","+_longitude);
        request.getUrl().put("radius",_radius);
        request.getUrl().put("sensor",false);
        if(types!=null)
            request.getUrl().put("types",types);

        PlacesList list=request.execute().parseAs(PlacesList.class);

        Log.d("Places Status",""+list.status);
        return list;
    }
    catch (HttpResponseException e)
    {
        Log.e("Error:",e.getMessage());
        return null;
    }
}

public PlaceDetails getPlaceDetails(String reference) throws Exception
{
    try
    {
        HttpRequestFactory httpRequestFactory = createRequestFactory(HTTP_TRANSPORT);
        HttpRequest request=httpRequestFactory
                .buildGetRequest(new GenericUrl(PLACES_DETAILS_URL));
        request.getUrl().put("key",API_KEY);
        request.getUrl().put("reference",reference);
        request.getUrl().put("sensor","false");

        PlaceDetails place = request.execute().parseAs(PlaceDetails.class);
        return place;
    }
    catch (HttpResponseException e)
    {
        Log.e("Error in Place Details",e.getMessage());
        throw e;
    }
}

public static HttpRequestFactory createRequestFactory(
        final HttpTransport transport)
{
    return transport.createRequestFactory(new HttpRequestInitializer()
    {
        public void initialize(HttpRequest request)
        {
            GoogleHeaders headers=new GoogleHeaders();
            headers.setApplicationName("PocketAmbulance");
            request.setHeadres(headers);
            JSONObjectParser parser=new JsonHttpParser(new JacksonFactory());
            request.addParser(parser);
        }
    });
  }
}
1

There are 1 best solutions below

0
On

HttpClient was deprecated in API Level 22 and removed in API Level 23. If you still want to use it, add this on your gradle file the legacy library

android {
    useLibrary 'org.apache.http.legacy'
}