且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C#windows窗体和Java android app之间的交互

更新时间:2023-12-06 14:04:52

你觉得很老了方式,很遗憾地说。因为现在甚至客户都可以将数据发送到服务器,在很多方面,一种是旧的HTTP请求方式,其他方式包括Ajax请求。但这对你的问题来说几乎不是主题。您需要的是基于网络的平台,用于Android应用程序和基于Windows 8的Windows窗体应用程序之间的交互和通信。虽然它们完全不同,但它们之间需要相似之处。



一个简单的方法是使用一个网站,你可以使用 WebViewClient [ ^ ]视图,或者您可以为Android客户端开发应用程序并嵌入WebViewClient,它将导航到您的网站。例如,在此Android开发人员文档 [ ^ ],它显示了在Android中创建Web应用程序的一个非常基本的示例。之后,您可以将数据推送回Windows窗体。



现在让我们想一下WebService。以WCF为例。你有一个.NET框架应用程序,这就是为什么使用另一个.NET应用程序不会给你带来财富的原因。假设您创建了一个可以通过网络从其他客户端使用的Web服务; Web服务具有客户端连接并执行不同操作的IP地址,您可以使用网络定义的IP,例如localhost(可以在您自己的路由器上运行),也可以购买稳定的IP地址并将其设置为Web为客户提供连接的服务。你可以阅读这篇文章 [ ^ 我的a>],创建一个简单的WCF Web服务,供网站(或作为客户端在.NET框架上运行的任何其他框架)使用。



一旦处理完毕,您就可以创建一个网站,通过该网站您将访问Web服务以将数据发布到Web服务(不会充当集线器),然后您将数据推送到Win表单同样,如果您通过Win表单使用该服务,则可以通过您的中心中心将数据传输到Android应用程序。所以现在实际的代码将在集线器(Web服务)上执行,并将数据推送到客户端。



你可以阅读这篇文章,以获得更深入的
使用双工WCF服务广播事件 [ ^ ]。文章能够解释使用相同的WCF中心集线器从客户端向客户端发送数据时使用的方法。您应该注意的一件事是,客户端(网站)和主机(WCF)是运行在.NET上的应用程序的一部分。正如在您的问题的评论中,如果您想创建一个将使用Java或任何其他框架的应用程序,您将不得不离开.NET并且必须为端口和网络(低级格式)工作,希望C ++能够对你来说,这是一个很好的语言。否则,.NET框架和Java都有一个很好的API来处理网络和其他端口,您可以使用它们通过网络直接连接您的应用程序,而无需在它们之间使用任何其他介质。 .NET框架网络命名空间 [ ^ ],与Java网络相关的API [ ^ ],如果你想继续这样做,这些对你都有帮助。
You think in a very old way, sorry to say that. Because now adays even clients can send the data to the server, in many ways, one is an old way of HTTP requests, others include Ajax requests. But that is pretty much off-topic for your question. What you need is a network based platform, for interaction and communication between the Android application and a Windows 8 based Windows Form application. Although they're totally different, and would require something to be similar between them.

A simple way of doing this can be using a website, which you can surf to using the WebViewClient[^] view inside any Android application, or you can develop an application for the Android client and embed a WebViewClient, that would navigate to only your website. For example, at this Android developer document[^], it is shown a very basic example of creating a web app in Android. After that, you can push the data back to the Windows Forms.

Let us now think of a WebService. Take WCF, for example. You're having a .NET framework application, that is why using another .NET application won't cause you a fortune. Let's say you create a web service that can be consumed from other clients, through the network; a web service has an IP address to which clients connect and perform different actions, you can either use a network defined IP such as localhost (which would work on your own router) or you can purchase a stable IP address and set it to the web service for the clients to connect to. You can read this article[^] of mine, to create a simple WCF web service to be consumed by a website (or any other framework running over .NET framework as a client).

Once that has been taken care of, you can create a website, through which you will access the web service for posting the data to the web service (which will not act as a hub) and then you will push the data to the Win form, similarly if you're using the service through Win form, you can transfer the data to the Android application through that central hub of yours. So now the actual code would execute at the hub (web service) and which will push the data to the clients.

You can read this article, to get more in-depth knowledge of Broadcasting Events with a Duplex WCF Service[^]. Article is capable of explaining the methods used in sending the data from clients to clients using the same WCF central hub. One thing you should note is, that the client (website) and the host (WCF) are a part of applications that are running over .NET. As in comments to your question, if you want to create an application that would be using Java or any other framework you would have to leave .NET and would have to work for ports and networks (in low-level format) hopefully, C++ would be a great langauge for you then. Otherwise, .NET framework and Java both have a great API for working with networks and other ports, that you can use to directly connect your application to each other over the network without using any other medium between them. .NET framework network namespace[^], and the Java network related APIs[^], these both would be helpful for you if you want to continue this way.