1

我一直在开发 android 应用程序,在使用google places api时,它运行良好。当我在模拟器上运行时,所有预期的结果都是从HTTP请求中检索的。但是当我使用释放键将它导出到设备时,它崩溃了。我发现问题在于 Google Places api 检索结果,错误代码有时是零结果和无效请求。unfortunately app stopped working

我尝试了所有可能的方法来解决这个问题,但无法解决这个问题。这是代码的样子:

public PlacesList search(double latitude, double longitude, double radius, String searchtypes) 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("rankby",  "distance");
            request.getUrl().put("radius", _radius); // in meters
            request.getUrl().put("sensor", "false");
            if(searchtypes != null)
                request.getUrl().put("types", searchtypes);

            PlacesList list = request.execute().parseAs(PlacesList.class);
            // Check log cat for places response status
            //Log.d("Places Status", "" + list.status);
            return list;

        } catch (HttpResponseException e) {
            Log.e("Error:", e.getMessage());
            return null;
        }

    }

    /**
     * Searching single place full details
     * @param refrence - reference id of place
     *                 - which you will get in search api request
     * */
    public PlaceDetails getPlaceDetails(String reference) throws Exception {
        try {
            //Log.v("reference " , reference);
            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);
            Log.v("place successfully bought " , place.result.name);
            return place;

        } catch (HttpResponseException e) {
            Log.e("Error in Perform Details", e.getMessage());
            throw e;
        }
    }

    /**
     * Creating http request Factory
     * */
    public static HttpRequestFactory createRequestFactory(
            final HttpTransport transport) {
        return transport.createRequestFactory(new HttpRequestInitializer() {
            public void initialize(HttpRequest request) {
                GoogleHeaders headers = new GoogleHeaders();
                headers.setApplicationName("TreasureRun");
                request.setHeaders(headers);
                JsonHttpParser parser = new JsonHttpParser(new JacksonFactory());
                request.addParser(parser);
            }
        });
    }

The above code are present in GooglePlaces.java file and it is called from async task present in java2 file.

The code in java2 file is:

@Override
protected String doInBackground(String... args) {
 // creating Places class object 
 String taskstatus;
 googlePlaces = new GooglePlaces();
 try {
     String keyword = "cafe|restaurant";
     double radius = 1000; // 1000 meters 
     nearPlaces = googlePlaces.search(gps.getLatitude(),
     gps.getLongitude(), radius, keyword);
 } catch (Exception e) {
    e.printStackTrace();
}
taskstatus = "done";
return taskstatus;
}

但发生的事情是我打电话的时候:

status = nearPlaces.status;

我猜真实设备中的状态不是“Ok”并且它正在崩溃。但在模拟器中它完美流畅

我试了很多天。任何人请帮助我

谢谢纳文

4

0 回答 0