试图将事件添加到谷歌的地方

时间:2012-11-22 05:44:15

标签: google-maps google-places-api

我正在尝试使用Google Place Actions API,特别是这些活动,但我无法获得有效的帖子。

以下是我使用的网址:

https://maps.googleapis.com/maps/api/place/event/add/json?sensor=false&key=placesApiKey&duration=26000&reference=CjQwAAAAv4TTQ3ySXiGhOElWFNAQ-roLOfgwo215yRTk1Bmhg0jSJ-sAdz9nHgNgnGBAmqP7EhC7K0AjTfFcZgCUh68c2yNtGhRkmynXvE5d4XA5ZfyBqAxlNdsAIg&summary=this会变得有趣

参考是Tempe,AZ。我一直得到404回来说这是非法请求。任何帮助都会很棒!我真的不知道我做错了什么。

我尝试了三种不同的方法,结果相同:

HttpClient client = new HttpClient();
client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());

String url = "https://maps.googleapis.com/maps/api/place/event/add/json?sensor=false&key=" + googlePlacesAPIKey;
PostMethod post = new PostMethod(url);
NameValuePair[] data = {
        new NameValuePair("duration", Long.toString(duration)),
        new NameValuePair("reference", reference),
        new NameValuePair("summary", summary)
    };

post.setRequestBody(data);

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("https://maps.googleapis.com/maps/api/place/event/add/json?sensor=false&key=" + googlePlacesAPIKey);

try {

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("duration", Long.toString(duration)));
    nameValuePairs.add(new BasicNameValuePair("reference", reference));
    nameValuePairs.add(new BasicNameValuePair("summary", summary));

    post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    HttpResponse response = client.execute(post);
}

URL url = new URL("https://maps.googleapis.com/maps/api/place/event/add/json?sensor=false&key="+googlePlacesAPIKey+"&duration="+duration+"&reference="+reference+"&summary="+summary);
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestMethod("POST");
OutputStreamWriter out = new OutputStreamWriter( httpCon.getOutputStream());
System.out.println(httpCon.getResponseCode());
System.out.println(httpCon.getResponseMessage()); out.close();

HttpPost post = new HttpPost("https://maps.googleapis.com/maps/api/place/event/add/json?sensor=false&key=" + googlePlacesAPIKey);
post.setHeader("Content-type", "application/json");

JSONObject object = new JSONObject();

object.put("duration", Long.toString(duration));
object.put("reference", reference);
object.put("summary", summary);

String message = object.toString();

post.setEntity(new StringEntity(message));

HttpResponse response = client.execute(post);

以下是那些好奇的API的链接:

https://developers.google.com/places/documentation/actions#event_add

2 个答案:

答案 0 :(得分:0)

在python中,你可以这样做:

#!/usr/bin/python
# coding: utf8

import sys
import urllib

parameters = urllib.urlencode({
    'key' : "YOUR_API_KEY",
    'sensor' : 'false'
  })
url = "https://maps.googleapis.com/maps/api/place/event/add/json?%s" % (parameters)

#The reference
reference = "CoQBdgAAAN4u...YKmgQ"

#Add event
postdata = '''
{
  "duration": 86400,
  "language": "ja",
  "reference": "%s",
  "summary": "Event Name!",
  "url" : "http://hogehoge.com/test_page"
}
''' % (reference)

f = urllib.urlopen(url, postdata)
print f.read()

答案 1 :(得分:0)

好吧,我对Java不好,我为Android Java创建了一个示例代码。

[MainActivity.java]

package com.example.placeseventtest;

import org.json.JSONArray;
import org.json.JSONObject;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
  private final String API_KEY = "YOUR_API_KEY";
  private PlacesHTTP myUtil;

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

    TextView resTxtView = (TextView) findViewById(R.id.responseText);
    myUtil = new PlacesHTTP(API_KEY, resTxtView);


    Button currentPosBtn = (Button) this.findViewById(R.id.currentPosBtn);
    currentPosBtn.setOnClickListener(new OnClickListener() {
      public void onClick(View view) {
        JSONObject params = new JSONObject();
        JSONObject location = new JSONObject();
        JSONArray types = new JSONArray();
        try {
          location.put("lat", 123.4556);
          location.put("lng", 123.4556);

          params.put("location", location);
          params.put("accuracy", 20);
          params.put("name", "Event Name");

          //only one type is available.
          types.put("parking");
          params.put("types", types);

          params.put("language", "en");
        } catch (Exception e) {}


        // Show the request JSON data.
        TextView reqTxtView = (TextView) findViewById(R.id.requestText);
        try {
          reqTxtView.setText(params.toString(2));
        } catch (Exception e) {}

        // POST to Google Server
        myUtil.execute(params);
      }
    });
  }
}

[PlacesHTTP.java]

package com.example.placeseventtest;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.widget.TextView;

public class PlacesHTTP extends AsyncTask<JSONObject, Void, HttpResponse>{

  private HttpPost post;
  private HttpClient httpClient;
  private String url;
  private TextView txtView;

  public PlacesHTTP(String api_key, TextView resultView) {
    url = String.format("https://maps.googleapis.com/maps/api/place/add/json?sensor=false&key=%s", api_key);
    txtView = resultView;
  }

  protected void onPreExecute() {
    httpClient = new DefaultHttpClient();
    post = new HttpPost(url);
    post.setHeader("Accept", "application/json");
    post.setHeader("Content-type", "application/json");
  }


  @Override
  protected HttpResponse doInBackground(JSONObject... params) {
    //Send data as JSON format
    JSONObject opts = params[0];
    StringEntity strEntity;
    HttpResponse response = null;
    try {
      strEntity = new StringEntity(opts.toString());
      post.setEntity(strEntity);
      response = httpClient.execute(post);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return response;
  }

  protected void onPostExecute(HttpResponse result) {
    if (result != null) {
      // Display the result
      try {
        txtView.setText(EntityUtils.toString(result.getEntity()));
      } catch (Exception e) {
        e.printStackTrace();
      }
    } else {
        txtView.setText("null");
    }
  }
}

我得到了这个结果:

enter image description here