- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在使用 SignalR Java Client在一个 Android 项目中连接到在我的本地主机上运行的服务器(只是一个用 C# 编写的控制台应用程序)。以下是我在 Android 端的代码,在主 Activity 的 onCreate
方法中。该应用程序在模拟器上运行,因此我将 URL 的本地主机部分更改为“10.0.2.2”。
try {
AndroidPlatformComponent com = new AndroidPlatformComponent();
Platform.loadPlatformComponent(com);
String url = "http://10.0.2.2:8080/signalr";
HubConnection connection = new HubConnection(url);
HubProxy hubProxy = connection.createHubProxy("MyHub");
connection.start().get();
hubProxy.on("Send", new SubscriptionHandler2<String, String>() {
@Override
public void run(String e1, String e2) {
Log.d("Server", e1.toString() + " -> " + e2.toString());
}
}, String.class, String.class);
}catch (Exception ex){
ex.printStackTrace();
}
每次我在行 connection.start().get();
上收到以下错误:
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err: java.util.concurrent.ExecutionException: microsoft.aspnet.signalr.client.transport.NegotiationException: There was a problem in the negotiation with the server
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err: at microsoft.aspnet.signalr.client.SignalRFuture.get(SignalRFuture.java:112)
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err: at microsoft.aspnet.signalr.client.SignalRFuture.get(SignalRFuture.java:102)
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err: at com.izmyr.projectx.main.MainActivity$2.onClick(MainActivity.java:85)
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err: at android.view.View.performClick(View.java:4084)
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err: at android.view.View$PerformClick.run(View.java:16966)
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err: at android.os.Handler.handleCallback(Handler.java:615)
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err: at android.os.Handler.dispatchMessage(Handler.java:92)
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err: at android.os.Looper.loop(Looper.java:137)
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err: at android.app.ActivityThread.main(ActivityThread.java:4745)
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err: at java.lang.reflect.Method.invoke(Method.java:511)
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err: at dalvik.system.NativeStart.main(Native Method)
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err: Caused by: microsoft.aspnet.signalr.client.transport.NegotiationException: There was a problem in the negotiation with the server
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err: at microsoft.aspnet.signalr.client.transport.HttpClientTransport$1.onResponse(HttpClientTransport.java:86)
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err: at microsoft.aspnet.signalr.client.http.java.NetworkRunnable.run(NetworkRunnable.java:82)
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err: at java.lang.Thread.run(Thread.java:856)
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err: Caused by: microsoft.aspnet.signalr.client.http.InvalidHttpStatusCodeException: Invalid status code: 400
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err: Response: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err: <HTML><HEAD><TITLE>Bad Request</TITLE>
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err: <META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err: <BODY><h2>Bad Request - Invalid Hostname</h2>
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err: <hr><p>HTTP Error 400. The request hostname is invalid.</p>
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err: </BODY></HTML>
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err: Headers: [null: HTTP/1.1 400 Bad Request; ]; [Date: Wed, 14 Oct 2015 20:01:57 GMT; ]; [Content-Length: 334; ]; [X-Android-Received-Millis: 1444852816135; ]; [Connection: close; ]; [Content-Type: text/html; charset=us-ascii; ]; [Server: Microsoft-HTTPAPI/2.0; ]; [X-Android-Sent-Millis: 1444852816126; ];
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err: at microsoft.aspnet.signalr.client.transport.HttpClientTransport.throwOnInvalidStatusCode(HttpClientTransport.java:206)
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err: at microsoft.aspnet.signalr.client.transport.HttpClientTransport$1.onResponse(HttpClientTransport.java:76)
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err: ... 2 more
我试图从 onCreate
中获取代码并将其放入 AsyncTask
中,但它也不起作用。
我需要尽快得到任何类型的建议/解决方案。
编辑:以下是我的简单服务器端代码:
using System;
using Microsoft.AspNet.SignalR;
using Microsoft.Owin.Hosting;
using Owin;
using Microsoft.Owin.Cors;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// This will *ONLY* bind to localhost, if you want to bind to all addresses
// use http://*:8080 to bind to all addresses.
// See http://msdn.microsoft.com/en-us/library/system.net.httplistener.aspx
// for more information.
string url = "http://localhost:8080";
using (WebApp.Start(url))
{
Console.WriteLine("Server running on {0}", url);
var context = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
var key = Console.ReadLine();
while (key != "quit")
{
context.Clients.All.Send("Server", key);
key = Console.ReadLine();
}
}
}
}
class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseCors(CorsOptions.AllowAll);
app.MapSignalR();
}
}
public class MyHub : Hub
{
public void Receive(string name, string message)
{
Console.WriteLine(name + " " + message);
}
public void Send(string name, string message)
{
Clients.All.addMessage(name, message);
}
}
}
最佳答案
从你的 logcat "...Caused by: microsoft.aspnet.signalr.client.http.InvalidHttpStatusCodeException: Invalid status code: 400..." and "...HTTP错误 400。请求主机名无效...”。
因此,请先检查您的服务器应用程序。从您的 PC/笔记本电脑的 Web 浏览器访问 http://ip:8080/signalr
(ip 是您 PC 的 IP 地址)而不是 http://localhost:8080/signalr
并查看结果。
然后,从您的 android 客户端,用该 IP 替换 10.0.2.2。
关于java - SignalR Java 客户端 - 启动 HubConnection 时出现 NegotiationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33134799/
我正在使用 SignalR Java Client在一个 Android 项目中连接到在我的本地主机上运行的服务器(只是一个用 C# 编写的控制台应用程序)。以下是我在 Android 端的代码,在主
我是一名优秀的程序员,十分优秀!