我正在尝试在 Java ME MIDP 应用程序中读取来自 Google Sheets API 的 JSON 回复。我用其他地址尝试了以下操作,它可以很好地接收它们的内容,但我要使用的实际 API 是 Sheets,它总是返回“证书验证失败”异常。
HttpConnection c = null;
InputStream is = null;
StringBuffer str = new StringBuffer();
try
{
c = (HttpsConnection)Connector.open(urlstring);
c.setRequestMethod(HttpConnection.GET);
c.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
c.setRequestProperty("User-Agent","Profile/MIDP-2.1 Configuration/CLDC-1.1");
is = c.openInputStream();
int len = (int)c.getLength();
int ch;
while ( (ch = is.read() ) != -1)
{
str.append((char)ch);
}
}
catch( Exception e ){ alert( ""+e ); }
return str.toString();
如果 URL 以 Https 开头,Connector.open() 会隐式返回一个 HttpsConnection,因此它应该仍然可以工作。
HTTPS 请求示例
https://jsonplaceholder.typicode.com/posts/1
这不起作用,但上面也允许 HTTP 连接
http://jsonplaceholder.typicode.com/posts/1
哪个会起作用。
但是,Google 表格需要 HTTPS,因此无法通过上述代码获得。如何通过 HTTPS 向工作表 API 发出 GET 请求?谢谢你。