0

我正在尝试将 Twilio 客户端集成到更大的应用程序中。在我打电话之前,一切似乎都很好device.connect(parameters, connectionListener)。我得到了31100 Generic Malformed Request错误,就是这样。

在同一设备上,使用相同的 Twilio 帐户和相同的 Twilio 应用程序,随 Twilio Android SDK (MonkeyPhone) 提供的示例代码可以完美运行。

我找不到有关错误含义或可能原因的更多详细信息。虽然我假设我正在发送无效数据,但我不明白这怎么可能。Capability Token 没问题,我已经根据 MonkeyPhone 示例应用程序中生成的那个进行了验证。创建一个Device工作正常,没有错误。即使我没有在connect()方法中发送任何参数,也会引发错误。的onConnecting()方法ConnectionListener被调用,但随后onDisconnected(Connection inConnection, int inErrorCode, String inErrorMessage)调用Malformed Request错误。

Voice TwiML 的代码运行良好,它只是一个简单的 PHP 脚本,可以生成最简单的<Dial>动词:

<Response>
    <Dial>someone</Dial>
</Response>

其他具体信息...我在我的应用程序中运行另一个服务,用于执行各种其他操作。这会以某种方式干扰吗?另外,我使用的是试用帐户,我住在罗马尼亚,那里不支持拨打真实电话号码(但我反正不使用电话号码)。这会以任何方式影响我吗?

我为抛出巨大的代码墙提前道歉,但我希望第二双眼睛能发现错误。这是与 MonkeyPhone 示例最相似的代码版本。唯一的区别是我使用 AsyncTask 来获取功能令牌(JsonAsyncRequestWithError类。

public class MonkeyPhone implements Twilio.InitListener, DeviceListener {
private static final String TAG = "MonkeyPhone";

private Context context;
private Device device;
private Connection connection;

public MonkeyPhone(Context context) {
    this.context = context;
    Twilio.initialize(context, this /* Twilio.InitListener */);
}

@Override
/* Twilio.InitListener method */
public void onInitialized() {
    Log.d(TAG, "Twilio SDK is ready");

    // the Emulator has a somewhat unique "product" name
    String clientName = "doug";

    HttpGet get = new HttpGet("http://teamphoenix.zzl.org/capability.php?ClientName=" + clientName);

    JsonAsyncRequestWithError asyncRequestWithError = new JsonAsyncRequestWithError(context, "test", new AsyncRequestWithErrorListener() {

        @Override
        public void onResult(AsyncRequestResponse response, Object destination) {
            createDevice(response.getMessage());
        }

        @Override
        public void onErrorResult(AsyncRequestResponse response, Object destination) {

        }
    });

    asyncRequestWithError.execute(get);

}

public void createDevice(String token) {
    try {
        device = Twilio.createDevice(token, this /* DeviceListener */);

        Intent intent = new Intent(context, SpringshotPhoneActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        device.setIncomingIntent(pendingIntent);

    } catch (Exception e) {
        Log.e(TAG, "", e);
    }
}

@Override
/* Twilio.InitListener method */
public void onError(Exception e) {
    Log.e(TAG, "Twilio SDK couldn't start: " + e.getLocalizedMessage());
}

@Override
/* DeviceListener method */
public void onStartListening(Device inDevice) {
    Log.i(TAG, "Device is now listening for incoming connections");
}

@Override
/* DeviceListener method */
public void onStopListening(Device inDevice) {
    Log.i(TAG, "Device is no longer listening for incoming connections");
}

@Override
/* DeviceListener method */
public void onStopListening(Device inDevice, int inErrorCode, String inErrorMessage) {
    Log.i(TAG, "Device is no longer listening for incoming connections due to error " + inErrorCode + ": " + inErrorMessage);
}

@Override
/* DeviceListener method */
public boolean receivePresenceEvents(Device inDevice) {
    return false; // indicate we don't care about presence events
}

@Override
/* DeviceListener method */
public void onPresenceChanged(Device inDevice, PresenceEvent inPresenceEvent) {
}

public void connect(String phoneNumber) {
    Map<String, String> parameters = new HashMap<String, String>(1);
    parameters.put("PhoneNumber", phoneNumber);
/// ---------------- THIS IS THE CALL THAT FAILS ------------------------------------//
    connection = device.connect(parameters, null /* ConnectionListener */);
    if (connection == null)
        Log.w(TAG, "Failed to create new connection");
}

public void disconnect() {
    if (connection != null) {
        connection.disconnect();
        connection = null;
    }
}

public void handleIncomingConnection(Device inDevice, Connection inConnection) {
    Log.i(TAG, "Device received incoming connection");
    if (connection != null)
        connection.disconnect();
    connection = inConnection;
    connection.accept();
}

@Override
protected void finalize() {
    if (connection != null)
        connection.disconnect();
    if (device != null)
        device.release();
}
}

非常感谢!

4

1 回答 1

2

我解决了这个问题。显然,我必须使用 UTF-8 编码从服务器读取 InputStream(即使令牌中没有特殊字符)。

    char[] buf = new char[1024];
    InputStream is = response.getEntity().getContent();
    StringBuilder out = new StringBuilder();

    Reader in = new InputStreamReader(is, "UTF-8");

    int bin;
    while ((bin = in.read(buf, 0, buf.length)) >= 0) {
        out.append(buf, 0, bin);
    }

    return out.toString();
于 2012-07-24T08:30:30.160 回答