我在android中使用webrtc进行视频通话,但我得到的是html响应而不是json,我的通话url是;https://apprtc.appspot.com/?r=111111"
这是我的代理客户。我遇到了致命错误: 无法将java.lang.String类型的值DOCTYPE转换为JSONObject GetParameters错误URL(URL[0]) RoomParameterGetter类中的这一行我在android中使用webrtc进行视频通话,但我得到的是html响应而不是json,我的通话url是;https://apprtc.appspot.com/?r=111111",android,webrtc,Android,Webrtc,这是我的代理客户。我遇到了致命错误: 无法将java.lang.String类型的值DOCTYPE转换为JSONObject GetParameters错误URL(URL[0]) RoomParameterGetter类中的这一行 公共类apptclient{ 私有静态最终字符串标记=“AppTclient”; 私人GAEChannelClient频道客户端; 私人最终活动; private final GAEChannelClient.MessageHandler gaeHandler; 专用
公共类apptclient{
私有静态最终字符串标记=“AppTclient”;
私人GAEChannelClient频道客户端;
私人最终活动;
private final GAEChannelClient.MessageHandler gaeHandler;
专用最终IceServersObserver IceServersObserver;
//这些成员仅在sendQueue的锁下读/写。
私有LinkedList sendQueue=新LinkedList();
私人进近信号参数进近信号参数;
/**
*一旦房间的信令参数指定了
*要使用的ICE服务器。
*/
公共静态接口IceServersObserver{
公共服务器(列出ICEServer);
}
公众咨询客户(活动、,
GAEChannelClient.MessageHandler gaeHandler,
IceServersObserver(IceServersObserver){
这个。活动=活动;
this.gaeHandler=gaeHandler;
this.iceServersObserver=iceServersObserver;
}
/**
*异步连接到AppTC房间URL,例如。
* https://apprtc.appspot.com/?r=NNN 并注册消息处理回调
*在它的GAE频道上。
*/
公共无效连接空间(字符串url){
while(url.indexOf(“?”)<0){
//继续重定向,直到我们得到房间号码。
(新建重定向解析程序()).execute(url);
return;//上面的RedirectResolver使用下一个URL返回给我们。
}
(新建RoomParameterGetter()).execute(url);
}
/**
*断开与GAE通道的连接。
*/
公共空间断开连接(){
if(channelClient!=null){
channelClient.close();
channelClient=null;
}
}
/**
*将要发送到会议室频道的消息排队,如果已经发送,则发送
*已连接(当通道关闭时,其他wise队列消息将被清空
*最终成立)。
*/
公共同步无效发送消息(字符串消息){
已同步(发送队列){
添加(msg);
}
RequestQueueDrainBackground();
}
公共布尔isInitiator(){
返回ApprSignalingParameters.initiator;
}
公共媒体约束pcConstraints(){
返回通知信号参数.pcConstraints;
}
公共媒体约束videoConstraints(){
返回AppTCSignalingParameters.videoConstraints;
}
公共媒体约束音频约束(){
返回ApprSignalingParameters.audioConstraints;
}
//持有进近室信号参数的结构。
专用类ApprSignalingParameters{
公开最终名单;
公共最终字符串gaeBaseHref;
公共最终字符串通道令牌;
公共最终字符串postMessageUrl;
公共最终布尔启动器;
公共最终媒体约束和培训;
公共最终媒体限制;
公共最终媒体限制条件;
公共许可信号参数(
列出iceServers,字符串gaeBaseHref,
字符串channelToken、字符串postMessageUrl、布尔启动器、,
媒体约束和培训,
媒体约束和视频约束,
媒体约束(音频约束){
this.iceServers=iceServers;
this.gaeBaseHref=gaeBaseHref;
this.channelToken=channelToken;
this.postMessageUrl=postMessageUrl;
this.initiator=启动器;
this.pcConstraints=pcConstraints;
this.videoConstraints=videoConstraints;
this.audioConstraints=audioConstraints;
}
}
//加载给定的URL并返回
//结果302响应。如果结果不是302,则抛出。
私有类重定向解析程序扩展异步任务{
@凌驾
受保护的字符串doInBackground(字符串…URL){
如果(url.length!=1){
抛出新的RuntimeException(“必须使用单个URL调用”);
}
试一试{
返回followRedirect(URL[0]);
}捕获(IOE异常){
抛出新的运行时异常(e);
}
}
@凌驾
受保护的void onPostExecute(字符串url){
连接空间(url);
}
私有字符串followRedirect(字符串url)引发IOException{
HttpURLConnection连接=(HttpURLConnection)新URL(URL)
.openConnection();
connection.setInstanceFollowDirections(false);
int code=connection.getResponseCode();
if(code!=HttpURLConnection.HTTP\u MOVED\u TEMP){
抛出新IOException(“意外响应:+code+”for”
+url+,包含以下内容:
+drainStream(connection.getInputStream());
}
int n=0;
字符串名称、值;
while((name=connection.getHeaderFieldKey(n))!=null){
值=connection.getHeaderField(n);
如果(名称等于(“位置”)){
返回值;
}
++n;
}
抛出新IOException(“未找到位置标头!”);
}
}
//AsyncTask,它将AppTC房间URL转换为一组信号
//用于该房间的参数。
私有类RoomParameterGetter扩展
异步任务{
@凌驾
受保护的ApprSignalingParameters doInBackground(字符串…URL){
如果(url.length!=1){
抛出新的RuntimeException(“必须使用单个URL调用”);
}
试一试{
返回GetParametersFroromUrl(URL[0]);
}捕获(JSONException e){
抛出新的运行时异常(e);
}捕获(IOE异常){
抛出新的运行时异常(e);
}
}
@凌驾
受保护的void onPostExecute(ApprtSignalingParameters参数){
channelClient=新的GAEChannelClient(活动,第
public class AppRTCClient {
private static final String TAG = "AppRTCClient";
private GAEChannelClient channelClient;
private final Activity activity;
private final GAEChannelClient.MessageHandler gaeHandler;
private final IceServersObserver iceServersObserver;
// These members are only read/written under sendQueue's lock.
private LinkedList<String> sendQueue = new LinkedList<String>();
private AppRTCSignalingParameters appRTCSignalingParameters;
/**
* Callback fired once the room's signaling parameters specify the set of
* ICE servers to use.
*/
public static interface IceServersObserver {
public void onIceServers(List<PeerConnection.IceServer> iceServers);
}
public AppRTCClient(Activity activity,
GAEChannelClient.MessageHandler gaeHandler,
IceServersObserver iceServersObserver) {
this.activity = activity;
this.gaeHandler = gaeHandler;
this.iceServersObserver = iceServersObserver;
}
/**
* Asynchronously connect to an AppRTC room URL, e.g.
* https://apprtc.appspot.com/?r=NNN and register message-handling callbacks
* on its GAE Channel.
*/
public void connectToRoom(String url) {
while (url.indexOf('?') < 0) {
// Keep redirecting until we get a room number.
(new RedirectResolver()).execute(url);
return; // RedirectResolver above calls us back with the next URL.
}
(new RoomParameterGetter()).execute(url);
}
/**
* Disconnect from the GAE Channel.
*/
public void disconnect() {
if (channelClient != null) {
channelClient.close();
channelClient = null;
}
}
/**
* Queue a message for sending to the room's channel and send it if already
* connected (other wise queued messages are drained when the channel is
* eventually established).
*/
public synchronized void sendMessage(String msg) {
synchronized (sendQueue) {
sendQueue.add(msg);
}
requestQueueDrainInBackground();
}
public boolean isInitiator() {
return appRTCSignalingParameters.initiator;
}
public MediaConstraints pcConstraints() {
return appRTCSignalingParameters.pcConstraints;
}
public MediaConstraints videoConstraints() {
return appRTCSignalingParameters.videoConstraints;
}
public MediaConstraints audioConstraints() {
return appRTCSignalingParameters.audioConstraints;
}
// Struct holding the signaling parameters of an AppRTC room.
private class AppRTCSignalingParameters {
public final List<PeerConnection.IceServer> iceServers;
public final String gaeBaseHref;
public final String channelToken;
public final String postMessageUrl;
public final boolean initiator;
public final MediaConstraints pcConstraints;
public final MediaConstraints videoConstraints;
public final MediaConstraints audioConstraints;
public AppRTCSignalingParameters(
List<PeerConnection.IceServer> iceServers, String gaeBaseHref,
String channelToken, String postMessageUrl, boolean initiator,
MediaConstraints pcConstraints,
MediaConstraints videoConstraints,
MediaConstraints audioConstraints) {
this.iceServers = iceServers;
this.gaeBaseHref = gaeBaseHref;
this.channelToken = channelToken;
this.postMessageUrl = postMessageUrl;
this.initiator = initiator;
this.pcConstraints = pcConstraints;
this.videoConstraints = videoConstraints;
this.audioConstraints = audioConstraints;
}
}
// Load the given URL and return the value of the Location header of the
// resulting 302 response. If the result is not a 302, throws.
private class RedirectResolver extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
if (urls.length != 1) {
throw new RuntimeException("Must be called with a single URL");
}
try {
return followRedirect(urls[0]);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
protected void onPostExecute(String url) {
connectToRoom(url);
}
private String followRedirect(String url) throws IOException {
HttpURLConnection connection = (HttpURLConnection) new URL(url)
.openConnection();
connection.setInstanceFollowRedirects(false);
int code = connection.getResponseCode();
if (code != HttpURLConnection.HTTP_MOVED_TEMP) {
throw new IOException("Unexpected response: " + code + " for "
+ url + ", with contents: "
+ drainStream(connection.getInputStream()));
}
int n = 0;
String name, value;
while ((name = connection.getHeaderFieldKey(n)) != null) {
value = connection.getHeaderField(n);
if (name.equals("Location")) {
return value;
}
++n;
}
throw new IOException("Didn't find Location header!");
}
}
// AsyncTask that converts an AppRTC room URL into the set of signaling
// parameters to use with that room.
private class RoomParameterGetter extends
AsyncTask<String, Void, AppRTCSignalingParameters> {
@Override
protected AppRTCSignalingParameters doInBackground(String... urls) {
if (urls.length != 1) {
throw new RuntimeException("Must be called with a single URL");
}
try {
return getParametersForRoomUrl(urls[0]);
} catch (JSONException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
protected void onPostExecute(AppRTCSignalingParameters params) {
channelClient = new GAEChannelClient(activity, params.channelToken,
gaeHandler);
synchronized (sendQueue) {
appRTCSignalingParameters = params;
}
requestQueueDrainInBackground();
iceServersObserver
.onIceServers(appRTCSignalingParameters.iceServers);
}
// Fetches |url| and fishes the signaling parameters out of the JSON.
private AppRTCSignalingParameters getParametersForRoomUrl(String url)
throws IOException, JSONException {
url = url + "&t=json";
JSONObject roomJson = new JSONObject(drainStream((new URL(url))
.openConnection().getInputStream()));
if (roomJson.has("error")) {
JSONArray errors = roomJson.getJSONArray("error_messages");
throw new IOException(errors.toString());
}
String gaeBaseHref = url.substring(0, url.indexOf('?'));
String token = roomJson.getString("token");
String postMessageUrl = "/message?r="
+ roomJson.getString("room_key") + "&u="
+ roomJson.getString("me");
boolean initiator = roomJson.getInt("initiator") == 1;
LinkedList<PeerConnection.IceServer> iceServers = iceServersFromPCConfigJSON(roomJson
.getString("pc_config"));
boolean isTurnPresent = false;
for (PeerConnection.IceServer server : iceServers) {
if (server.uri.startsWith("turn:")) {
isTurnPresent = true;
break;
}
}
if (!isTurnPresent) {
iceServers
.add(requestTurnServer(roomJson.getString("turn_url")));
}
MediaConstraints pcConstraints = constraintsFromJSON(roomJson
.getString("pc_constraints"));
addDTLSConstraintIfMissing(pcConstraints);
Log.d(TAG, "pcConstraints: " + pcConstraints);
MediaConstraints videoConstraints = constraintsFromJSON(getAVConstraints(
"video", roomJson.getString("media_constraints")));
Log.d(TAG, "videoConstraints: " + videoConstraints);
MediaConstraints audioConstraints = constraintsFromJSON(getAVConstraints(
"audio", roomJson.getString("media_constraints")));
Log.d(TAG, "audioConstraints: " + audioConstraints);
return new AppRTCSignalingParameters(iceServers, gaeBaseHref,
token, postMessageUrl, initiator, pcConstraints,
videoConstraints, audioConstraints);
}
// Mimic Chrome and set DtlsSrtpKeyAgreement to true if not set to false
// by
// the web-app.
private void addDTLSConstraintIfMissing(MediaConstraints pcConstraints) {
for (MediaConstraints.KeyValuePair pair : pcConstraints.mandatory) {
if (pair.getKey().equals("DtlsSrtpKeyAgreement")) {
return;
}
}
for (MediaConstraints.KeyValuePair pair : pcConstraints.optional) {
if (pair.getKey().equals("DtlsSrtpKeyAgreement")) {
return;
}
}
// DTLS isn't being suppressed (e.g. for debug=loopback calls), so
// enable
// it by default.
pcConstraints.optional.add(new MediaConstraints.KeyValuePair(
"DtlsSrtpKeyAgreement", "true"));
}
// Return the constraints specified for |type| of "audio" or "video" in
// |mediaConstraintsString|.
private String getAVConstraints(String type,
String mediaConstraintsString) {
try {
JSONObject json = new JSONObject(mediaConstraintsString);
// Tricksy handling of values that are allowed to be (boolean or
// MediaTrackConstraints) by the getUserMedia() spec. There are
// three
// cases below.
if (!json.has(type) || !json.optBoolean(type, true)) {
// Case 1: "audio"/"video" is not present, or is an explicit
// "false"
// boolean.
return null;
}
if (json.optBoolean(type, false)) {
// Case 2: "audio"/"video" is an explicit "true" boolean.
return "{\"mandatory\": {}, \"optional\": []}";
}
// Case 3: "audio"/"video" is an object.
return json.getJSONObject(type).toString();
} catch (JSONException e) {
throw new RuntimeException(e);
}
}
private MediaConstraints constraintsFromJSON(String jsonString) {
if (jsonString == null) {
return null;
}
try {
MediaConstraints constraints = new MediaConstraints();
JSONObject json = new JSONObject(jsonString);
JSONObject mandatoryJSON = json.optJSONObject("mandatory");
if (mandatoryJSON != null) {
JSONArray mandatoryKeys = mandatoryJSON.names();
if (mandatoryKeys != null) {
for (int i = 0; i < mandatoryKeys.length(); ++i) {
String key = mandatoryKeys.getString(i);
String value = mandatoryJSON.getString(key);
constraints.mandatory
.add(new MediaConstraints.KeyValuePair(key,
value));
}
}
}
JSONArray optionalJSON = json.optJSONArray("optional");
if (optionalJSON != null) {
for (int i = 0; i < optionalJSON.length(); ++i) {
JSONObject keyValueDict = optionalJSON.getJSONObject(i);
String key = keyValueDict.names().getString(0);
String value = keyValueDict.getString(key);
constraints.optional
.add(new MediaConstraints.KeyValuePair(key,
value));
}
}
return constraints;
} catch (JSONException e) {
throw new RuntimeException(e);
}
}
// Requests & returns a TURN ICE Server based on a request URL. Must be
// run
// off the main thread!
private PeerConnection.IceServer requestTurnServer(String url) {
try {
URLConnection connection = (new URL(url)).openConnection();
connection.addRequestProperty("user-agent", "Mozilla/5.0");
connection.addRequestProperty("origin",
"https://apprtc.appspot.com");
String response = drainStream(connection.getInputStream());
JSONObject responseJSON = new JSONObject(response);
String uri = responseJSON.getJSONArray("uris").getString(0);
String username = responseJSON.getString("username");
String password = responseJSON.getString("password");
return new PeerConnection.IceServer(uri, username, password);
} catch (JSONException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
// Return the list of ICE servers described by a WebRTCPeerConnection
// configuration string.
private LinkedList<PeerConnection.IceServer> iceServersFromPCConfigJSON(
String pcConfig) {
try {
JSONObject json = new JSONObject(pcConfig);
JSONArray servers = json.getJSONArray("iceServers");
LinkedList<PeerConnection.IceServer> ret = new LinkedList<PeerConnection.IceServer>();
for (int i = 0; i < servers.length(); ++i) {
JSONObject server = servers.getJSONObject(i);
String url = server.getString("urls");
String credential = server.has("credential") ? server
.getString("credential") : "";
ret.add(new PeerConnection.IceServer(url, "", credential));
}
return ret;
} catch (JSONException e) {
throw new RuntimeException(e);
}
}
// Request an attempt to drain the send queue, on a background thread.
private void requestQueueDrainInBackground() {
(new AsyncTask<Void, Void, Void>() {
public Void doInBackground(Void... unused) {
maybeDrainQueue();
return null;
}
}).execute();
}
// Send all queued messages if connected to the room.
private void maybeDrainQueue() {
synchronized (sendQueue) {
if (appRTCSignalingParameters == null) {
return;
}
try {
for (String msg : sendQueue) {
URLConnection connection = new URL(
appRTCSignalingParameters.gaeBaseHref
+ appRTCSignalingParameters.postMessageUrl)
.openConnection();
connection.setDoOutput(true);
connection.getOutputStream().write(msg.getBytes("UTF-8"));
if (!connection.getHeaderField(null).startsWith(
"HTTP/1.1 200 ")) {
throw new IOException("Non-200 response to POST: "
+ connection.getHeaderField(null)
+ " for msg: " + msg);
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
sendQueue.clear();
}
}
// Return the contents of an InputStream as a String.
private static String drainStream(InputStream in) {
Scanner s = new Scanner(in).useDelimiter("\\A");
return s.hasNext() ? s.next() : "";
}
}