1

所以我试图在我的 android 应用程序中使用 Google Places API,但它一直在崩溃。在测试了不同的东西之后,我发现它在尝试实例化时会崩溃。

package com.fender.isittoolate;

import java.io.IOException;
import com.gmail.yuyang226.flickr.places.PlacesList;
import com.google.api.client.googleapis.GoogleHeaders;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.HttpResponseException;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.apache.ApacheHttpTransport;
import com.google.api.client.http.json.JsonHttpParser;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.client.http.javanet.NetHttpTransport;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class IsItTooLateActivity extends Activity {
private final static String API_KEY = "key_here";
private final static String BASE_SEARCH_URL = "https://maps.googleapis.com/maps/api/place/search/json?";
private final NetHttpTransport transport = new NetHttpTransport();

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final Button button = (Button) findViewById(R.id.resultButton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    double longitude = location.getLongitude();
    double latitude = location.getLatitude();
    GenericUrl url = new GenericUrl(BASE_SEARCH_URL);
    url.put("key", API_KEY);
    url.put("location", latitude + "," + longitude);
    url.put("sensor", "true");
    url.put("radius", "500");
}

public HttpRequestFactory createRequestFactory(final HttpTransport transport) {
    return transport.createRequestFactory(new HttpRequestInitializer() {
        public void initialize(HttpRequest request) {
            GoogleHeaders headers = new GoogleHeaders();
            headers.setApplicationName("Google-Places-DemoApp");
            request.setHeaders(headers);
            JsonHttpParser parser = new JsonHttpParser(null);
            // parser.setJsonFactory(new JacksonFactory());
            request.addParser(parser);
        }
    });
}
});

}
}

这是我用的。有人有什么建议吗?我在网上看到的其他演示并没有真正的帮助......

4

0 回答 0