如何通过使用Android或Curl发送POST请求在Google站点中创建新内容?
目前,我正致力于创建一个Android应用程序来阅读和创建谷歌网站的内容。看起来JavaAPI不适用于Android,所以我使用google协议 . 我可以通过这个get请求获取google站点上的所有内容 . 但我不知道如何发送POST请求,在GoogleAPI指南旁创建内容。 希望有人能帮我。我所需要的就是如何以curl格式发送请求。如何通过使用Android或Curl发送POST请求在Google站点中创建新内容?,android,http,post,curl,http-headers,Android,Http,Post,Curl,Http Headers,目前,我正致力于创建一个Android应用程序来阅读和创建谷歌网站的内容。看起来JavaAPI不适用于Android,所以我使用google协议 . 我可以通过这个get请求获取google站点上的所有内容 . 但我不知道如何发送POST请求,在GoogleAPI指南旁创建内容。 希望有人能帮我。我所需要的就是如何以curl格式发送请求。 谢谢 您可以像这样尝试curl命令: curl -H "Content-Type: your-content-type" -X POST -d 'your-d
谢谢 您可以像这样尝试curl命令:
curl -H "Content-Type: your-content-type" -X POST -d 'your-data' https://localhost:8080/api/login
要使用HttpUrlConnection类发布数据,请使用以下命令:
HttpURLConnection connection = null;
try {
//Create connection
url = new URL(targetURL);
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"your-content-type");//set required content type
connection.setRequestProperty("Content-Length", "" +
data.getBytes().length);//set Content-Length header using your data length in bytes
connection.setRequestProperty("Content-Language", "en-US");
connection.setDoInput(true);
connection.setDoOutput(true);
//Send request
DataOutputStream wr = new DataOutputStream (
connection.getOutputStream ());
wr.writeBytes (data);//write data to o/p stream
wr.flush ();
wr.close ();
//Get Response
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
return response.toString();
} catch (Exception e) {
} finally {
if(connection != null) {
connection.disconnect();
}
}