我正在使用Google Play Services库登录Google Plus。有了这个库,我可以检索访问令牌,向谷歌服务(如日历)发送请求。
我已经收到了一个事件的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请求的方法。有人知道在不使用日历库的情况下完成这项工作的方法吗?
谢谢!
下面是一个带有httpURLConnection的post请求,用于模拟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;
}
}