如何在signarjava客户机中捕获异常

如何在signarjava客户机中捕获异常,java,android,signalr,signalr.client,Java,Android,Signalr,Signalr.client,我在Asp.net web api中运行signalr hub。我可以通过从点网络客户端调用中心和客户端函数来发送和接收消息。现在我正试图在java客户端(Android平台)上实现同样的功能。我设法连接到中心并接收消息。然而,当我发送消息时,它抛出一个异常。发送消息时(调用集线器函数),集线器函数被成功调用,另一方也接收消息。但是,来自中心的响应(返回给发送方)是html格式的,而不是json格式的。这会在客户端引发异常。我可以暂时忽略异常,因为消息正在传递。但是,我无法捕获异常,因此andr

我在Asp.net web api中运行signalr hub。我可以通过从点网络客户端调用中心和客户端函数来发送和接收消息。现在我正试图在java客户端(Android平台)上实现同样的功能。我设法连接到中心并接收消息。然而,当我发送消息时,它抛出一个异常。发送消息时(调用集线器函数),集线器函数被成功调用,另一方也接收消息。但是,来自中心的响应(返回给发送方)是html格式的,而不是json格式的。这会在客户端引发异常。我可以暂时忽略异常,因为消息正在传递。但是,我无法捕获异常,因此android进程被终止

客户端代码:

private HubConnection mConnection;
private HubProxy mProxy;

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    initializeSignalr();
    connectNotificationHub();

    Button btnSend = (Button) findViewById(R.id.btnSend);
    View.OnClickListener oclBtnSend = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                mProxy.invoke("SendToOperators", "21", "test message", "chat").done(new Action<Void>() {
                    @Override
                    public void run(Void obj) throws Exception {
                        System.out.println("SENT!");
                    }
                }).onError(new ErrorCallback() {
                    @Override
                    public void onError(Throwable throwable) {
                        System.out.println("ERROR!");
                    }
                }).get();
            } catch (Exception e) {

            } 
        }
    };
    btnSend.setOnClickListener(oclBtnSend);
}

    public void initializeSignalr(){
    Platform.loadPlatformComponent(new AndroidPlatformComponent());
}

public void ShutdownSignalr(){
    if(mConnection!=null){
        //mConnection.disconnect();
        mConnection.stop();
        mConnection=null;
    }
}

public void connectNotificationHub()
{
    String server = REST_SERVICE_URL+"signalr";
    mConnection = new HubConnection(server,"userName="+IMUSER_PFX+"_driver"+Integer.toString(DriverID),true, new Logger() {
        @Override
        public void log(String message, LogLevel level) {
            if (level == LogLevel.Critical) {
            Log.d("SIGNALR", level.toString() + ": " + message);
            }
        }
    });

    mProxy = mConnection.createHubProxy("NotificationHub");

    mProxy.subscribe(new Object() {
        @SuppressWarnings("unused")
        public void addMessage(String name, String message) {
            Log.d("Message: ", message);
        }
    });

    mConnection.error(new ErrorCallback() {
        @Override
        public void onError(Throwable error) {
            error.printStackTrace();
        }
    });

    SignalRFuture<Void> awaitConnection = mConnection.start();
    try {
        awaitConnection.get();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }
}
在java客户端中发送消息时

try {
            mProxy.invoke("SendToOperators", "21", "test message", "chat").done(new Action<Void>() {
                @Override
                public void run(Void obj) throws Exception {
                    System.out.println("SENT!");
                }
            }).onError(new ErrorCallback() {
                @Override
                public void onError(Throwable throwable) {
                    System.out.println("ERROR!");
                }
            }).get();
        } catch (Exception e) {

        }
我在另一边收到消息。但是中心的响应是html格式的,而不是json格式的。我确信这是一个bug,我将把它发布在github信号器问题跟踪器上。然而,我需要一种方法来阻止android客户端崩溃

来自中心的html响应

11-22 03:42:27.766  19661-20016/com.eb5.uvexa.uvexadriver D/SIGNALR﹕ Verbose: serverSentEvents - Response received
11-22 03:42:27.766  19661-20016/com.eb5.uvexa.uvexadriver D/SIGNALR﹕ Verbose: serverSentEvents - Read response to the end
11-22 03:42:27.776  19661-20016/com.eb5.uvexa.uvexadriver D/SIGNALR﹕ Verbose: serverSentEvents - Trigger onData with data: {"I":"1"}<!DOCTYPE html>
<html>
<head>
<title>Runtime Error</title>
<meta name="viewport" content="width=device-width" />
<style>
body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;}
p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px}
b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px}
H1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red }
H2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon }
pre {font-family:"Consolas","Lucida Console",Monospace;font-size:11pt;margin:0;padding:0.5em;line-height:14pt}
.marker {font-weight: bold; color: black;text-decoration: none;}
.version {color: gray;}
.error {margin-bottom: 10px;}
.expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; }
@media screen and (max-width: 639px) {
pre { width: 440px; overflow: auto; white-space: pre-wrap; word-wrap: break-word; }
}
@media screen and (max-width: 479px) {
pre { width: 280px; }
}
</style>
</head>
<body bgcolor="white">
<span><H1>Server Error in '/' Application.<hr width=100% size=1 color=silver></H1>
<h2> <i>Runtime Error</i> </h2></span>
<font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif ">
<b> Description: </b>An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server machine.
<br><br>
<b>Details:</b> To enable the details of this specific error message to be viewable on remote machines, please create a &lt;customErrors&gt; tag within a &quot;web.config&quot; configuration file located in the root directory of the current web application. This &lt;customErrors&gt; tag should then have its &quot;mode&quot; attribute set to &quot;Off&quot;.<br><br>
<table width=100% bgcolor="#ffffcc">
<tr>
<td>
<code><pre>
&lt;!-- Web.Config Configuration File --&gt;
&lt;configuration&gt;
&lt;system.web&gt;
&lt;customErrors mode=&quot;Off&quot;/&gt;
&lt;/system.web&gt;
&lt;/configuration&gt;</pre></code>
</td>
</tr>
</table>
<br>
<b>Notes:</b> The current error page you are seeing can be replaced by a custom error page by modifying the &quot;defaultRedirect&quot; attribute of the application&#39;s &lt;customErrors&gt; configuration tag to point to a custom error page URL.<br><br>
<table width=100% bgcolor="#ffffcc">
<tr>
<td>
<code><pre>
&lt;!-- Web.Config Configuration File --&gt;
&lt;configuration&gt;
&lt;system.web&gt;
&lt;customErrors mode=&quot;RemoteOnly&quot; defaultRedirect=&quot;mycustompage.htm&quot;/&gt;
&lt;/system.web&gt;
&lt;/configuration&gt;</pre></code>
</td>
</tr>
</table>
<br>
</body>
</html>
11-22 03:42:27.766 19661-20016/com.eb5.uvexa.uvexa驱动器D/信号器﹕ 详细:serverSentEvents-收到响应
11-22 03:42:27.766 19661-20016/com.eb5.uvexa.uvexa驱动器D/信号器﹕ Verbose:serverSentEvents-将响应读取到末尾
11-22 03:42:27.776 19661-20016/com.eb5.uvexa.uvexa驱动器D/信号器﹕ Verbose:serverSentEvents-使用以下数据触发onData:{“I”:“1”}
运行时错误
正文{字体系列:“Verdana”;字体重量:正常;字体大小:.7em;颜色:黑色;}
p{字体系列:“Verdana”;字体大小:正常;颜色:黑色;页边顶部:-5px}
{字体系列:“Verdana”;字体大小:粗体;颜色:黑色;页边顶部:-5px}
H1{字体系列:“Verdana”;字体重量:正常;字体大小:18pt;颜色:红色}
H2{字体系列:“Verdana”;字体重量:正常;字体大小:14pt;颜色:褐红色}
前{字体系列:“控制台”,“Lucida控制台”,单空格;字体大小:11pt;边距:0;填充:0.5em;线条高度:14pt}
.marker{字体大小:粗体;颜色:黑色;文本装饰:无;}
.version{color:gray;}
.错误{页边距底部:10px;}
.expandable{文本装饰:下划线;字体大小:粗体;颜色:海军蓝;光标:手;}
@媒体屏幕和屏幕(最大宽度:639px){
pre{width:440px;溢出:自动;空白:预换行;换行:断开单词;}
}
@媒体屏幕和屏幕(最大宽度:479px){
前{宽度:280px;}
}
“/”应用程序中出现服务器错误。
运行时错误 描述:服务器上发生应用程序错误。此应用程序的当前自定义错误设置阻止远程查看应用程序错误的详细信息(出于安全原因)。但是,本地服务器上运行的浏览器可以查看它。

详细信息:要在远程计算机上查看此特定错误消息的详细信息,请在当前web应用程序根目录中的“web.config”配置文件中创建customErrors标记。然后,此customErrors标记的“mode”属性应设置为“Off”。


!-- Web.Config配置文件--
配置
system.web
customErrors mode=“关闭”/
/system.web
/配置

注意:通过修改应用程序的“defaultRedirect”属性',可以用自定义错误页替换当前看到的错误页;s customErrors配置标记指向自定义错误页面URL。


!-- Web.Config配置文件--
配置
system.web
customErrors mode=“RemoteOnly”defaultRedirect=“mycustompage.htm”/
/system.web
/配置


我设法解决了这个问题。web服务返回了一个HTML格式的错误。我在服务服务器端解决了这个问题。

HubProxy是否可以配置为返回json?我认为这是signalr sdk上的一个错误(可能在服务器端或客户端)。当我收到消息时,我会收到json对象,比如发现新数据:数据:{“C”:“d-3CFAC619-B,0 | k,1 | l,2 | m,0”,“m”:[{“H”:“NotificationHub”,“m”:“addMessage”,“A”:[“redlineNew_Operator 3”,“赶快”]}}只有当我从客户端向中心发送消息时,它才会返回html。这就是为什么我只是试图捕捉异常以防止崩溃。我已经提交了bug。但我不认为这是一个积极的项目。我设法解决了这个问题。web服务返回了一个HTML格式的错误。我在服务服务器端解决了这个问题。是的,我曾经处理过一个服务器,它总是以XML格式返回错误,即使您请求JSON响应,所以我必须以XML风格完成所有工作。有点讨厌。很高兴你把它解决了。
11-22 03:42:27.776  19661-20016/com.eb5.uvexa.uvexadriver D/SIGNALR﹕ Critical: HubConnection - Error: com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Expected EOF at line 1 column 10
11-22 03:42:27.786  19661-20016/com.eb5.uvexa.uvexadriver W/System.err﹕ com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Expected EOF at line 1 column 10
11-22 03:42:27.786  19661-20016/com.eb5.uvexa.uvexadriver W/System.err﹕ at com.google.gson.JsonParser.parse(JsonParser.java:65)
11-22 03:42:27.786  19661-20016/com.eb5.uvexa.uvexadriver W/System.err﹕ at com.google.gson.JsonParser.parse(JsonParser.java:45)
11-22 03:42:27.786  19661-20016/com.eb5.uvexa.uvexadriver W/System.err﹕ at microsoft.aspnet.signalr.client.transport.TransportHelper.processReceivedData(TransportHelper.java:41)
11-22 03:42:27.786  19661-20016/com.eb5.uvexa.uvexadriver W/System.err﹕ at microsoft.aspnet.signalr.client.Connection.processReceivedData(Connection.java:733)
11-22 03:42:27.786  19661-20016/com.eb5.uvexa.uvexadriver W/System.err﹕ at microsoft.aspnet.signalr.client.Connection.access$0(Connection.java:728)
11-22 03:42:27.786  19661-20016/com.eb5.uvexa.uvexadriver W/System.err﹕ at microsoft.aspnet.signalr.client.Connection$1.onData(Connection.java:302)
11-22 03:42:27.786  19661-20016/com.eb5.uvexa.uvexadriver W/System.err﹕ at microsoft.aspnet.signalr.client.transport.HttpClientTransport$2.onResponse(HttpClientTransport.java:122)
11-22 03:42:27.786  19661-20016/com.eb5.uvexa.uvexadriver W/System.err﹕ at microsoft.aspnet.signalr.client.http.java.NetworkRunnable.run(NetworkRunnable.java:82)
11-22 03:42:27.786  19661-20016/com.eb5.uvexa.uvexadriver W/System.err﹕ at java.lang.Thread.run(Thread.java:864)
11-22 03:42:27.786  19661-20016/com.eb5.uvexa.uvexadriver W/System.err﹕ Caused by: com.google.gson.stream.MalformedJsonException: Expected EOF at line 1 column 10
11-22 03:42:27.786  19661-20016/com.eb5.uvexa.uvexadriver W/System.err﹕ at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1310)
11-22 03:42:27.786  19661-20016/com.eb5.uvexa.uvexadriver W/System.err﹕ at com.google.gson.stream.JsonReader.peek(JsonReader.java:390)
11-22 03:42:27.786  19661-20016/com.eb5.uvexa.uvexadriver W/System.err﹕ at com.google.gson.JsonParser.parse(JsonParser.java:60)
11-22 03:42:27.786  19661-20016/com.eb5.uvexa.uvexadriver W/System.err﹕ ... 8 more
11-22 03:42:27.766  19661-20016/com.eb5.uvexa.uvexadriver D/SIGNALR﹕ Verbose: serverSentEvents - Response received
11-22 03:42:27.766  19661-20016/com.eb5.uvexa.uvexadriver D/SIGNALR﹕ Verbose: serverSentEvents - Read response to the end
11-22 03:42:27.776  19661-20016/com.eb5.uvexa.uvexadriver D/SIGNALR﹕ Verbose: serverSentEvents - Trigger onData with data: {"I":"1"}<!DOCTYPE html>
<html>
<head>
<title>Runtime Error</title>
<meta name="viewport" content="width=device-width" />
<style>
body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;}
p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px}
b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px}
H1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red }
H2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon }
pre {font-family:"Consolas","Lucida Console",Monospace;font-size:11pt;margin:0;padding:0.5em;line-height:14pt}
.marker {font-weight: bold; color: black;text-decoration: none;}
.version {color: gray;}
.error {margin-bottom: 10px;}
.expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; }
@media screen and (max-width: 639px) {
pre { width: 440px; overflow: auto; white-space: pre-wrap; word-wrap: break-word; }
}
@media screen and (max-width: 479px) {
pre { width: 280px; }
}
</style>
</head>
<body bgcolor="white">
<span><H1>Server Error in '/' Application.<hr width=100% size=1 color=silver></H1>
<h2> <i>Runtime Error</i> </h2></span>
<font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif ">
<b> Description: </b>An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server machine.
<br><br>
<b>Details:</b> To enable the details of this specific error message to be viewable on remote machines, please create a &lt;customErrors&gt; tag within a &quot;web.config&quot; configuration file located in the root directory of the current web application. This &lt;customErrors&gt; tag should then have its &quot;mode&quot; attribute set to &quot;Off&quot;.<br><br>
<table width=100% bgcolor="#ffffcc">
<tr>
<td>
<code><pre>
&lt;!-- Web.Config Configuration File --&gt;
&lt;configuration&gt;
&lt;system.web&gt;
&lt;customErrors mode=&quot;Off&quot;/&gt;
&lt;/system.web&gt;
&lt;/configuration&gt;</pre></code>
</td>
</tr>
</table>
<br>
<b>Notes:</b> The current error page you are seeing can be replaced by a custom error page by modifying the &quot;defaultRedirect&quot; attribute of the application&#39;s &lt;customErrors&gt; configuration tag to point to a custom error page URL.<br><br>
<table width=100% bgcolor="#ffffcc">
<tr>
<td>
<code><pre>
&lt;!-- Web.Config Configuration File --&gt;
&lt;configuration&gt;
&lt;system.web&gt;
&lt;customErrors mode=&quot;RemoteOnly&quot; defaultRedirect=&quot;mycustompage.htm&quot;/&gt;
&lt;/system.web&gt;
&lt;/configuration&gt;</pre></code>
</td>
</tr>
</table>
<br>
</body>
</html>