Я написал фрагмент кода, который загружает видео с телефона на YouTube.Код отлично работает в тестовом приложении, но когда я включаю его в приложение Phonegap, он выдает эту ошибку «Нет сертификата однорангового узла»
package org.apache.cordova;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import android.util.Log;
public class YouTubeUploader {
String DeveloperKey = "";
String YouTubeUsername = "";
String GoogleUsername = "";
String Password = "";
String Source = "";
private String AuthToken = "";
void Login() {
HttpClient httpclient = new DefaultHttpClient();
HttpPost request = new HttpPost(
"https://www.google.com/accounts/ClientLogin");
request.addHeader("Content-Type", "application/x-www-form-urlencoded");
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("Email", GoogleUsername));
params.add(new BasicNameValuePair("Passwd", Password));
params.add(new BasicNameValuePair("source", Source));
params.add(new BasicNameValuePair("service", "youtube"));
try {
request.setEntity(new UrlEncodedFormEntity(params));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
HttpResponse response = null;
try {
response = httpclient.execute(request);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
HttpEntity resEntityGet = response.getEntity();
try {
String responseText = EntityUtils.toString(resEntityGet);
String[] tokens = responseText.split("\n");
if (tokens.length >= 3) {
String[] authTokens = tokens[2].split("=");
this.AuthToken = authTokens[1];
}
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
boolean UploadVideo(String videoPath) {
File file = new File(videoPath);
String boundary = "qwerty";
String endLine = "\r\n";
StringBuilder sb = new StringBuilder();
sb.append("--" + boundary + endLine);
sb.append("Content-Type: application/atom+xml; charset=UTF-8" + endLine
+ endLine);
StringBuilder xml = new StringBuilder();
xml.append("<?xml version=\"1.0\"?>");
xml.append("<entry xmlns=\"http://www.w3.org/2005/Atom\" xmlns:media=\"http://search.yahoo.com/mrss/\" xmlns:yt=\"http://gdata.youtube.com/schemas/2007\">");
xml.append("<media:group>");
xml.append("<media:title type=\"plain\">" + file.getName()
+ "</media:title>");
xml.append("<media:description type=\"plain\">test video</media:description>");
xml.append("<media:category scheme=\"http://gdata.youtube.com/schemas/2007/categories.cat\">People</media:category>");
xml.append("<media:keywords>video</media:keywords>");
xml.append("</media:group>");
xml.append("</entry>");
sb.append(xml.toString() + endLine);
sb.append("--" + boundary + endLine);
sb.append("Content-Type: application/octet-stream" + endLine);
sb.append("Content-Transfer-Encoding: binary" + endLine + endLine);
String bodyStart = sb.toString();
sb = new StringBuilder();
sb.append(endLine + "--" + boundary + "--");
String bodyEnd = sb.toString();
HttpURLConnection conn;
try {
FileInputStream fIn = new FileInputStream(file);
byte fileBytes[] = new byte[(int) file.length()];
fIn.read(fileBytes);
conn = (HttpURLConnection) new URL(
"http://uploads.gdata.youtube.com/feeds/api/users/"
+ YouTubeUsername + "/uploads").openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type",
"multipart/related; boundary=\"" + boundary + "\"");
conn.setRequestProperty("Authorization", "GoogleLogin auth="
+ this.AuthToken);
conn.setRequestProperty("GData-Version", "2");
conn.setRequestProperty("X-GData-Key", "key=" + DeveloperKey);
conn.setRequestProperty("Slug", file.getName());
conn.setRequestProperty(
"Content-Length",
""
+ (bodyStart.getBytes().length + fileBytes.length + bodyEnd
.getBytes().length));
conn.setRequestProperty("Connection", "close");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
try {
conn.connect();
Log.d("ID", "" + file.length());
try {
OutputStream os = new BufferedOutputStream(
conn.getOutputStream());
os.write(bodyStart.getBytes());
os.write(fileBytes);
os.write(bodyEnd.getBytes());
os.flush();
String response = "";
try {
response = "Success! " + read(conn.getInputStream());
return true;
} catch (FileNotFoundException e) {
// Error Stream contains JSON that we can parse to a
// FB
// error
response = "Error!" + read(conn.getErrorStream());
}
Log.d("ID", response);
} catch (FileNotFoundException e1) {
Log.d("ID", e1.getMessage(), e1);
} catch (IOException e) {
Log.d("ID", e.getMessage(), e);
}
} catch (IOException e2) {
Log.d("ID", e2.getMessage(), e2);
}
} catch (MalformedURLException e3) {
Log.d("ID", e3.getMessage(), e3);
} catch (IOException e3) {
Log.d("ID", e3.getMessage(), e3);
}
return false;
}
String read(InputStream is) {
try {
byte[] buffer = new byte[is.available()];
is.read(buffer, 0, buffer.length);
return buffer.toString();
} catch (IOException e1) {
Log.d("ID", e1.getMessage(), e1);
}
return "";
}
}