我正在尝试进行API调用以获取此类YouTube视频列表:
@Override
protected String doInBackground(String... theParams)
{
String myUrl = theParams[0];
final String key = "my_key";
final String channelId = "UCoLEarNS6E-Kbzoya_p7k2Q";
String charset = "UTF-8";
String response = null;
try
{
String query = String.format("key=%s&channelId=%s&part=snippet,id&order=date&maxResults=20",
URLEncoder.encode( key, charset ) ,
URLEncoder.encode( channelId, charset )
);
final URL url = new URL( myUrl + "?" + query );
final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.connect();
final InputStream is = conn.getInputStream();
final byte[] buffer = new byte[8196];
int readCount;
final StringBuilder builder = new StringBuilder();
while ((readCount = is.read(buffer)) > -1)
{
builder.append(new String(buffer, 0, readCount));
}
response = builder.toString();
}
catch (IOException e)
{
sendEmail ( "MyQuestionsActivity Network Error" , "Error: " + e.getMessage() );
}
return response;
}
但它不断出错。如果我将网址粘贴到浏览器中,它会返回一些JSON。但在这种情况下,我发出的sendEmail实用程序是从e.getMessag()发送此错误:
Error: https://www.googleapis.com/youtube/v3/search?key=my_key&channelId=UCoLEarNS6E-Kbzoya_p7k2Q&part=snippet,id&order=date&maxResults=20
有人知道什么是不对的吗?我的catLog也遇到了一些问题,所以我无法看到异常。