我正在开发一个使用google places api的Android应用程序,但我获得了NETWORK_ERROR状态和状态代码7
这是我的代码:
private GoogleApiClient mGoogleApiClient;
@Override
protected void onStart() {
super.onStart();
if( mGoogleApiClient != null )
mGoogleApiClient.connect();
}
@Override
protected void onStop() {
if( mGoogleApiClient != null && mGoogleApiClient.isConnected() ) {
mGoogleApiClient.disconnect();
}
super.onStop();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, 0, this)
.addApi(Places.GEO_DATA_API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).build();
}
@Override
public void onConnectionFailed(ConnectionResult result) {
Toast.makeText(getApplicationContext(), "Connection Failed", Toast.LENGTH_SHORT).show();
}
@Override
public void onConnected(Bundle arg0) {
Toast.makeText(getApplicationContext(), "Connected", Toast.LENGTH_SHORT).show();
System.out.println("add place>>>>>");
System.out.println("add place>>>>>");
System.out.println("add place>>>>>");
System.out.println("add place>>>>>");
try {
addPlace();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onConnectionSuspended(int arg0) {
Toast.makeText(getApplicationContext(), "sConnection Suspended", Toast.LENGTH_SHORT).show();
}
private void addPlace() {
AddPlaceRequest place =
new AddPlaceRequest(
"My Custom Place", // Name
new LatLng(38.124606, 30.652658), // Latitude and longitude
"test address", // Address
Collections.singletonList(Place.TYPE_AQUARIUM),// Place types
"+1 650-253-0000" // phone nb
);
Places.GeoDataApi.addPlace(mGoogleApiClient, place)
.setResultCallback(new ResultCallback<PlaceBuffer>() {
@Override
public void onResult(PlaceBuffer places) {
System.out.println("places.getStatus().getStatus()>>>>>"+places.getStatus().getStatus());
System.out.println("places.getStatus().getStatusCode()>>>>>"+places.getStatus().getStatusCode());
System.out.println("places.getStatus().getStatusMessage()>>>>>"+places.getStatus().getStatusMessage());
System.out.println("places.getStatus().isSuccess()>>>>>"+places.getStatus().isSuccess());
System.out.println("places.getStatus()>string>>>>"+places.getStatus().toString());
places.release();
Toast.makeText(getApplicationContext(), "place added", Toast.LENGTH_SHORT).show();
}
});
}
清单:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
这是我的应用程序标签我创建了google地方的api密钥并启用了两个元数据标记:com.google.android.gms.version和com.google.android.geo.API_KEY