我在Android项目中使用SignalR Java Client来连接到我的localhost上运行的服务器(只是一个用C#编写的控制台应用程序)。以下是我在Android主要活动的onCreate
方法内的代码。应用程序在模拟器上运行,因此我将URL的localhost部分更改为" 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);
}
}
}
答案 0 :(得分:0)
从你的logcat &#34; ...引起:microsoft.aspnet.signalr.client.http.InvalidHttpStatusCodeException:无效的状态代码:400 ...&#34; 和&#34; ... HTTP错误400.请求主机名无效...&#34; 。
请先检查您的服务器应用。在您的PC /笔记本电脑的网络浏览器中,访问http://ip:8080/signalr
(使用IP是您电脑的IP地址)而不是http://localhost:8080/signalr
,然后查看结果。
然后,从您的Android客户端,用该IP替换10.0.2.2。
答案 1 :(得分:0)
要澄清一些事情。从接受的答案我得到了主意。
第1步:我在 applicationhost.config 文件中进行了更改,该文件位于 username \ Documents \ IISExpress \ config 位置。对于较旧的
第2步:查找此块(根据您的应用程序):
<site name="SignalRChat" id="11">
<application path="/" applicationPool="Clr4IntegratedAppPool">
<virtualDirectory path="/" physicalPath="D:\Local\Experiment\SignalRAndroid\SignalRAndroid-master\Src\SignalR Server\SignalRChat" />
</application>
<bindings>
<binding protocol="http" bindingInformation="*:52527:localhost" />
<binding protocol="http" bindingInformation="*:52527:*" />
</bindings>
</site>
- 步骤3:在绑定标记中添加以下行。
<binding protocol="http" bindingInformation="*:52527:*" />
它将如下所示
<site name="SignalRChat" id="11">
<application path="/" applicationPool="Clr4IntegratedAppPool">
<virtualDirectory path="/" physicalPath="D:\Local\Experiment\SignalRAndroid\SignalRAndroid-master\Src\SignalR Server\SignalRChat" />
</application>
<bindings>
<binding protocol="http" bindingInformation="*:52527:localhost" />
<binding protocol="http" bindingInformation="*:52527:*" />
</bindings>
</site>
1.保存applicationhost.config文件。
2.以管理员模式运行Visual Studio。 (必须)
3.相应地更改端口。
答案 2 :(得分:-1)
我通过在服务器端代码中用string url = "http://localhost:8080";
替换行string url = "http://*:8080";
并以管理员身份运行来解决了这个问题。