从Google地图请求JSON会给我REQUEST_DENIED

时间:2015-04-28 23:10:07

标签: java android json google-maps google-places-api

我想知道为什么我的请求会被拒绝。 我很确定我的API密钥是正确的,我在Google Console中选择了android,用我的项目名称给了我sha1并抓住了API密钥并将其粘贴在我的项目中。但是,如果我尝试任何东西,它仍然会给我REQUEST_DENIED。我假设它与我如何构建URL或基本URL被破坏有关,或者它可能是我请求数据的方式。

我对此非常陌生并且几天前才开始玩,所以任何帮助都会受到赞赏。

这是我的代码 -

MainActivity

[CPTAnimation animate:pieChart property:@"startAngle" from:pieChart.startAngle to:pieChart.endAngle duration:1.0];

List_Activity

package io.github.invainn.quickeat;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.location.places.PlaceLikelihood;
import com.google.android.gms.location.places.PlaceLikelihoodBuffer;
import com.google.android.gms.location.places.Places;
import com.google.android.gms.maps.model.LatLng;


public class MainActivity extends ActionBarActivity implements AdapterView.OnItemSelectedListener, GoogleApiClient.OnConnectionFailedListener {

    Spinner spinner;
    GoogleApiClient mGoogleApiClient;

    private CharSequence mostLikelyPlace;
    private LatLng mostLikelyPlaceLatLng;

    private static final String LOG_TAG = "MainActivity";
    private static final int GOOGLE_API_CLIENT_ID = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mGoogleApiClient = new GoogleApiClient.Builder(MainActivity.this)
                .addApi(Places.PLACE_DETECTION_API)
                .enableAutoManage(this, GOOGLE_API_CLIENT_ID, this)
                .build();

        spinner = (Spinner) findViewById(R.id.spinner);

        ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.choices, android.R.layout.simple_spinner_item);
        spinner.setAdapter(adapter);

        setupSearchButton();

        PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi.getCurrentPlace(mGoogleApiClient, null);
        result.setResultCallback(new ResultCallback<PlaceLikelihoodBuffer>() {
            @Override
            public void onResult(PlaceLikelihoodBuffer likelyPlaces) {
                for (PlaceLikelihood placeLikelihood : likelyPlaces) {
                    Log.i(LOG_TAG, String.format("Place '%s' with " +
                                    "likelihood: %g",
                            placeLikelihood.getPlace().getName(),
                            placeLikelihood.getLikelihood()));
                            if(placeLikelihood.getLikelihood() >= .20) {
                                mostLikelyPlace = placeLikelihood.getPlace().getName();
                                mostLikelyPlaceLatLng = placeLikelihood.getPlace().getLatLng();
                            }
                }
                Toast.makeText(MainActivity.this, "Your current location: " + mostLikelyPlace, Toast.LENGTH_LONG).show();
                likelyPlaces.release();
            }
        });
    }

    private void setupSearchButton() {
        Button searchKeyword = (Button)findViewById(R.id.searchKeyword);

        searchKeyword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "Switched to ListActivity", Toast.LENGTH_SHORT).show();

                Bundle args = new Bundle();
                args.putParcelable("currentLocation", mostLikelyPlaceLatLng);

                Intent intent = new Intent(MainActivity.this, List_Activity.class);
                intent.putExtra("bundle", args);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {

    }
}

QueryRequest

package io.github.invainn.quickeat;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.Html;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.common.GooglePlayServicesRepairableException;
import com.google.android.gms.location.places.Place;
import com.google.android.gms.location.places.ui.PlacePicker;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;

import org.apache.http.HttpRequestFactory;

import java.net.URL;


public class List_Activity extends ActionBarActivity {
    private static final int PLACE_PICKER_REQUEST = 1;
    private static final String apiAddress = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_);

        // Receive bundle and get currentlocation
        Bundle bundle = getIntent().getParcelableExtra("bundle");
        LatLng currentLocation = bundle.getParcelable("currentLocation");

        new QueryRequest().execute(currentLocation);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_info, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

如果我的代码有任何其他问题,请做,我想知道。

3 个答案:

答案 0 :(得分:3)

请按照以下步骤操作:

Google控制台:

API&amp;验证 - &gt;启用API( Google Places API Web服务)。

API&amp;验证 - &gt; 凭据 - &gt; 创建新密钥 - &gt; 浏览器密钥(您需要创建浏览器密钥而不是Android密钥。)。

对于浏览器密钥,您不需要使用Sha1创建特定密钥。

答案 1 :(得分:1)

您需要启用以下位置api

- Google Places API网络服务

并且您需要使用服务器密钥来获取地点数据。

要获取服务器密钥,请按照以下步骤操作:   API&amp;验证 - &gt;凭证 - &gt;创建新密钥 - &gt;服务器密钥

浏览器密钥不起作用。

我遇到了同样的问题,并通过这样做解决了这个问题。

答案 2 :(得分:0)

请按照以下步骤操作:

Google Console Api:

(Google Places API网络服务)已启用。

API&amp;验证 - &gt;凭证 - &gt;创建新密钥 - &gt;

然后在键限制 ......

中选择无选项