我正在使用以下功能获取一个网址,以便从我的Android设备上传视频到Youtube:
班级变量:
private static final String INITIAL_UPLOAD_URL = "http://uploads.gdata.youtube.com/resumable/feeds/api/users/default/uploads";
功能:
private static String uploadMetaData(YoutubeUploadRequest uploadRequest, String clientLoginToken, Activity activity, boolean retry) {
System.out.println("Upload Request \n"+uploadRequest+"\n Client login token"+clientLoginToken+"\nActivity: "+"\nActivity name: "+activity+"\nRetry value:"+retry);
try {
File file = getFileFromUri(uploadRequest.getUri(), activity);
if(file != null) {
String uploadUrl = INITIAL_UPLOAD_URL;
System.out.println(uploadUrl);
URL url = new URL(uploadUrl);
System.out.println(url);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Authorization", String.format("GoogleLogin auth=\"%s\"", clientLoginToken));
connection.setRequestProperty("GData-Version", "2");
connection.setRequestProperty("X-GData-Client", CLIENT_ID);
connection.setRequestProperty("X-GData-Key", String.format("key=%s", DEVELOPER_KEY));
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/atom+xml");
connection.setRequestProperty("Slug", file.getAbsolutePath());
String title = uploadRequest.getTitle();
String description = uploadRequest.getDescription();
String category = uploadRequest.getCategory();
String tags = uploadRequest.getTags();
String template = readFile(activity, R.raw.gdata).toString();
String atomData = String.format(template, title, description, category, tags);
/*String template = readFile(activity, R.raw.gdata_geo).toString();
atomData = String.format(template, title, description, category, tags,
videoLocation.getLatitude(), videoLocation.getLongitude());*/
OutputStreamWriter outStreamWriter = new OutputStreamWriter(connection.getOutputStream());
outStreamWriter.write(atomData);
outStreamWriter.close();
int responseCode = connection.getResponseCode();
if (responseCode < 200 || responseCode >= 300) {
// The response code is 40X
if ((responseCode + "").startsWith("4") && retry) {
Log.d(TAG, "retrying to fetch auth token for ");
clientLoginToken = getClientAuthToken();
// Try again with fresh token
return uploadMetaData(uploadRequest, clientLoginToken, activity, false);
} else {
return null;
}
}
System.out.println(connection.getHeaderField("Location"));
return connection.getHeaderField("Location");
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("Message"+e);
}
return null;
}
我确定我传递给函数的参数,它们都是正确的。我通过调试交叉检查,我将此函数称为:
String uploadUrl = uploadMetaData(uploadRequest, authToken, activity, true);
但是我收到错误,格式错误的网址异常,我知道哪里可能出错?
这些是我传递的参数:
04-11 09:59:54.015: I/System.out(19815): com.xx.xxvideo.YoutubeUploadRequest@4257f060
04-11 09:59:54.016: I/System.out(19815): Client login tokenDQAAAHABAAAXXoe8KR8BuYY_0KE2cxW_lMzsUn7T81NbFsrF39OcQLmC-lpAjFCBv1EhBvzJFKp51e4Y8ad13YbRms6SifTDDhWyObvs7cDctKyMTHbgwFXKuZ6in-vBloUfYMLugjbF2fXfSqYehddquxNgaARVQFwd0Ks986MBJ0CceB1vcLSq31BNjWuG-mVHfhQbAj2xJoBHDaD-RAWDYJk4l0XzwFIIh3uP19TjeIocRQeeahchAi9hsl5UdPhG1WRXLpsCvXm-6vDt5NXzF3RrdUIKZlBNif_j0sSPO2jH1sSz6UfeEtSYblISdSx-XyJIRHF6f4dx7BlBa-QZ-30eY-mzTMNpYV2FSPP1WhIBVnGvGsAnOrJG-7vakx1upxGLgyFotD82Th23afQ38ax87GqX7gT-Vqk5elJ250WMHOHSYgoPGpe--B5PmBk3JPyT8wMtYycmAplJuuDFUDXuESnxPIlTpDpE3XFpljszQBQUPg
04-11 09:59:54.016: I/System.out(19815): Activity:
04-11 09:59:54.016: I/System.out(19815): Activity name: com.xx.xxvideo.MainActivity@42552cf0
04-11 09:59:54.017: I/System.out(19815): Retry value:true