title: How to catch exception in signalr java client
body: <p>I have signalr hub running in Asp.net web api. I can send and receive messages by calling hub and client functions from a dot net client. Now I am trying to implement the same on java client (Android platform). I managed to connect to the hub and receive the messages. However when I am sending message, it throws an exception. When sending messages (calling hub function), the hub function gets called successfully and the other side receives the message too. However the response (back to the sender) from the hub is in html format rather than json. That throws an exception on client side. I could ignore the exception for the time being as the messages are going through. However I am not able to catch the exception, as a result of this the android process gets terminated.</p>
  
  <p>Client code:</p>
  
  <pre><code>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&lt;Void&gt;() {
                      @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&lt;Void&gt; awaitConnection = mConnection.start();
      try {
          awaitConnection.get();
      } catch (InterruptedException e) {
          e.printStackTrace();
      } catch (ExecutionException e) {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>In dot net client it was not possible to catch exception with try catch clock if the server was not available however I was able to resolve it by subscribing to error callback </p>
  
  <pre><code>mConnection.error(new ErrorCallback() {
      @Override
      public void onError(Throwable error) {
          error.printStackTrace();
      }
  });
  </code></pre>
  
  <p>In java client when I am sending message</p>
  
  <pre><code>try {
              mProxy.invoke("SendToOperators", "21", "test message", "chat").done(new Action&lt;Void&gt;() {
                  @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) {
  
          }
  </code></pre>
  
  <p>it throwing json parsing exception</p>
  
  <pre><code>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
  </code></pre>
  
  <p>I am receiving the message on other side. But the response from the hub is in html format rather than json. I am sure it's a bug, which I will post on github signalr issue tracker. However I needed a way to stop the android client from crashing.</p>
  
  <p>The html response from the hub</p>
  
  <pre><code>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"}&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;Runtime Error&lt;/title&gt;
  &lt;meta name="viewport" content="width=device-width" /&gt;
  &lt;style&gt;
  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; }
  }
  &lt;/style&gt;
  &lt;/head&gt;
  &lt;body bgcolor="white"&gt;
  &lt;span&gt;&lt;H1&gt;Server Error in '/' Application.&lt;hr width=100% size=1 color=silver&gt;&lt;/H1&gt;
  &lt;h2&gt; &lt;i&gt;Runtime Error&lt;/i&gt; &lt;/h2&gt;&lt;/span&gt;
  &lt;font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif "&gt;
  &lt;b&gt; Description: &lt;/b&gt;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.
  &lt;br&gt;&lt;br&gt;
  &lt;b&gt;Details:&lt;/b&gt; To enable the details of this specific error message to be viewable on remote machines, please create a &amp;lt;customErrors&amp;gt; tag within a &amp;quot;web.config&amp;quot; configuration file located in the root directory of the current web application. This &amp;lt;customErrors&amp;gt; tag should then have its &amp;quot;mode&amp;quot; attribute set to &amp;quot;Off&amp;quot;.&lt;br&gt;&lt;br&gt;
  &lt;table width=100% bgcolor="#ffffcc"&gt;
  &lt;tr&gt;
  &lt;td&gt;
  &lt;code&gt;&lt;pre&gt;
  &amp;lt;!-- Web.Config Configuration File --&amp;gt;
  &amp;lt;configuration&amp;gt;
  &amp;lt;system.web&amp;gt;
  &amp;lt;customErrors mode=&amp;quot;Off&amp;quot;/&amp;gt;
  &amp;lt;/system.web&amp;gt;
  &amp;lt;/configuration&amp;gt;&lt;/pre&gt;&lt;/code&gt;
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
  &lt;br&gt;
  &lt;b&gt;Notes:&lt;/b&gt; The current error page you are seeing can be replaced by a custom error page by modifying the &amp;quot;defaultRedirect&amp;quot; attribute of the application&amp;#39;s &amp;lt;customErrors&amp;gt; configuration tag to point to a custom error page URL.&lt;br&gt;&lt;br&gt;
  &lt;table width=100% bgcolor="#ffffcc"&gt;
  &lt;tr&gt;
  &lt;td&gt;
  &lt;code&gt;&lt;pre&gt;
  &amp;lt;!-- Web.Config Configuration File --&amp;gt;
  &amp;lt;configuration&amp;gt;
  &amp;lt;system.web&amp;gt;
  &amp;lt;customErrors mode=&amp;quot;RemoteOnly&amp;quot; defaultRedirect=&amp;quot;mycustompage.htm&amp;quot;/&amp;gt;
  &amp;lt;/system.web&amp;gt;
  &amp;lt;/configuration&amp;gt;&lt;/pre&gt;&lt;/code&gt;
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
  &lt;br&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>