有没有办法在不使用库的情况下发送Google Calendar API的POST请求? Java的

时间:2014-10-24 18:21:24

标签: android calendar google-api google-play-services http-post

我使用Google Play服务库登录Google Plus。使用此库,我可以检索访问令牌,以便向Google服务发送请求,例如日历。

我已经收到了关于事件的GET请求,但我遇到了POST请求的问题。这是GET请求:

HttpURLConnection urlConnection = null;

    String resultJSON = null;

    ArrayList<Event> events = new ArrayList<Event>();

    URL url;
    try {
        url = new URL(baseurl + params[0]);

        urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestProperty("Authorization", "Bearer "
                + ProjectModel.getInstance().getOauthToken());

        resultJSON = CharStreams.toString(new InputStreamReader(
                urlConnection.getInputStream(), Charsets.UTF_8));

        if (!TextUtils.isEmpty(resultJSON)) {
            JSONArray resultArray;

            resultArray = new JSONObject(resultJSON).getJSONArray("items");

POST的URL与此请求相同:

https://www.googleapis.com/calendar/v3/calendars/id/events

问题是,我无法找到将其转换为POST请求的方法。任何人都知道如何在不使用日历库的情况下完成这项工作?

谢谢!

1 个答案:

答案 0 :(得分:0)

这是一个带有httpURLConnection的帖子请求,用于模拟gdata库的picasa后门程序。它应该与你的工作类似。我知道你发送JSON所以你必须用你的json代替xmlString并改变内容类型。

    private Document createAlbum() throws ParseException, IOException,
            ParserConfigurationException, SAXException {

        String xmlString = "<entry xmlns='http://www.w3.org/2005/Atom'"
                + " xmlns:media='http://search.yahoo.com/mrss/'"
                + " xmlns:gphoto='http://schemas.google.com/photos/2007'>"
                + "<title type='text'>"
                + GoPreferences.getString(context,
                        SettingRecord.ALBUM_NAME,
                        SettingRecord.ALBUM_NAME_DEFAULT)
                + "</title>"
                + "<summary type='text'>"
                + "This was the recent trip I made"
                + "</summary>"
                + "<gphoto:location></gphoto:location>"
                + "<gphoto:access>public</gphoto:access>"
                + "<gphoto:timestamp>"
                + Long.toString(System.currentTimeMillis())
                + "</gphoto:timestamp>"
                + "<media:group>"
                + "<media:keywords></media:keywords>"
                + "</media:group>"
                + "<category scheme='http://schemas.google.com/g/2005#kind'"
                + " term='http://schemas.google.com/photos/2007#album'></category>"
                + "</entry>";

        URL url = new URL(
                "https://picasaweb.google.com/data/feed/api/user/default");
        HttpURLConnection httpURLConnection = (HttpURLConnection) url
                .openConnection();
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setDoInput(true);
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setUseCaches(false);
        // clientid may not be necessary
        httpURLConnection.setRequestProperty("X-GData-Client", CLIENT_ID);
        httpURLConnection.setRequestProperty("GData-Version", "2");
        httpURLConnection.setRequestProperty("Authorization", "OAuth "
                + token);
        httpURLConnection.setRequestProperty("Content-Type",
                "application/atom+xml");

        OutputStream stream = httpURLConnection.getOutputStream();

        OutputStreamWriter sw = new OutputStreamWriter(stream, "UTF-8");

        sw.write(xmlString);
        sw.flush();
        sw.close();
        stream.close();

        if (httpURLConnection.getResponseCode() != 201) {
            return null;
        }

        InputStream result = httpURLConnection.getInputStream();
        DocumentBuilderFactory factory = DocumentBuilderFactory
                .newInstance();
        DocumentBuilder db = factory.newDocumentBuilder();
        Document doc = db.parse(result);

        String href = "";
        NodeList nodeList = doc.getElementsByTagName("link");
        int i = nodeList.getLength();
        for (int j = 0; j < i; j++) {
            Node node = nodeList.item(j);
            NamedNodeMap attr = node.getAttributes();
            if (attr.getNamedItem("rel").getTextContent().equals("edit")) {
                href = attr.getNamedItem("href").getTextContent();
                break;
            }
        }

        if (TextUtils.isEmpty(href)) {
            return null;
        } else {
            GoPreferences.putString(context, ALBUM_URL, href);
            return doc;
        }
}