Java 服务器和android之间的连接
我正在尝试开发一个即时消息应用程序(比如whatsapp)。我很困惑我应该使用什么类型的连接:套接字、http还是其他?我必须使用非标准JavaAPI吗?对于服务器,我使用java;对于客户端,我使用andoid(java)。 我已经尝试了套接字连接,但没有成功。我没有收到错误,但没有发生任何事情,因为设备和服务器没有连接(我不知道为什么) 这是我的客户端类(android),用于发送消息:Java 服务器和android之间的连接,java,android,sockets,http,instant-messaging,Java,Android,Sockets,Http,Instant Messaging,我正在尝试开发一个即时消息应用程序(比如whatsapp)。我很困惑我应该使用什么类型的连接:套接字、http还是其他?我必须使用非标准JavaAPI吗?对于服务器,我使用java;对于客户端,我使用andoid(java)。 我已经尝试了套接字连接,但没有成功。我没有收到错误,但没有发生任何事情,因为设备和服务器没有连接(我不知道为什么) 这是我的客户端类(android),用于发送消息: public class SendMsg implements Runnable { private
public class SendMsg implements Runnable {
private Socket socket;
String msg;
public SendMsg(String msg){
this.msg=msg;
}
@Override
public void run() {
try {
socket = new Socket("ip_globale_server",5000);
socket.setSoTimeout(5000);
BufferedWriter writer= new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
writer.write(msg);
writer.flush();
}
catch (IOException e){
System.err.println("Connection error");
}
finally{
if (socket!=null) {
try{socket.close();}
catch(IOException e){}
}
}
}
}
这是我的java服务器类,它等待连接:
public class ReceiveMsg implements Runnable {
ServerSocket server= null;
Socket connection=null;
InputStream in;
InputStreamReader inr;
StringBuilder smsg;
public void run() {
System.out.print("Server has started");
try {
server = new ServerSocket(5000);
System.out.println("I'm waiting for connection");
connection = server.accept();
System.out.println("I'm connected with "+ connection.toString());
in = connection.getInputStream();
inr = new InputStreamReader(in,"ASCII");
for (int c= inr.read();c!=-1; c= inr.read()){
smsg.append((char)c);
}
//this class is for storing the message
new Store (smsg.toString(),1,2);
}
catch(IOException ex){}
finally {
try{
if (server != null)
server.close();
if (server != null)
server.close();
}
catch(IOException e){
}
}
你能帮我开始吗。
提前感谢。您应该使用推送通知。如果您使用套接字,连接将在某个点终止,您将无法继续接收消息。对于android中的即时消息,您应该使用谷歌云消息 导言:
http://developer.android.com/google/gcm/index.html
入门:
http://developer.android.com/google/gcm/gs.html
我肯定会建议在这里使用
谷歌云消息
。这样做的好处是,一旦设备运行了你的应用程序,你就可以让它注册到GCM
上,你可以根据需要向他们发送消息,这样你就不必为一定时间后关闭的套接字而烦恼
这种方法需要您实现一些服务器(例如,使用PHP的web服务器)并让用户与其通信,因此流程如下:
GCM
服务注册BroadcastReceiver
来获取纬度和经度并将其发送到远程服务器,比如http://www.mysuperserver.com/getmsg.php
,通过POST
请求GCM
中针对您的项目注册设备。您可以通过在项目中导入Google Play Services
来实现这一点,完成后,执行类似的操作:
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
final String regid = gcm.register("YOUR_SENDER_ID");
GCM
服务器已经知道该用户已在您的项目中注册了whithin,并给了他一个regid
regid
保存在某个地方,以便您以后可以向他们发送消息。因此,在客户端,对发送regid
的服务器发出httppost
请求,并使用如下方式处理它:
$regid = $_POST['regid'];
if (($regId) && ($ipAddr))
sql_query("INSERT INTO gcm_subscribers(regid) VALUES($regid')");
选择所有用户并向他们发送新消息(模拟多播行为)
GCM
消息并相应地显示它。为了处理这些信息,我在下面添加了一些链接- 参考
- (西班牙语)
除此之外,我同意其他人关于gcm的看法为什么不使用谷歌云消息进行即时消息与谷歌云消息一起,你可以只发送消息或文件吗?你只能发送4KB的文本消息。下载一个文件,你可以把文件的链接发给他,让客户端负责下载。谢谢你,你的回答真的很有用,我不能投赞成票,因为我有点名气。很抱歉,没问题,只要在你决定使用哪种方法后,点击答案左侧的勾号,接受对你最有帮助的答案。我已经决定使用gmc。无论如何,谢谢你的回答。是的,你应该使用gcm。有些情况下,你认为你做不到。例如,如果要传输大于4k的数据(例如图像)。此外,也无法知道客户端是否通过gcm收到消息,因此在这种情况下,客户端应在收到消息时通知服务器。我可以使用两台服务器吗?一台用于ftp(发送照片、视频等媒体),另一台用于GMC?
function sendNotification($registrationIdsArray, $messageData) {
$apiKey = "YOUR_API_KEY";
$headers = array("Content-Type:" . "application/json", "Authorization:" . "key=" . $apiKey);
$data = array(
'data' => $messageData,
'registration_ids' => $registrationIdsArray
);
$ch = curl_init();
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch, CURLOPT_URL, "https://android.googleapis.com/gcm/send" );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode($data) );
$response = curl_exec($ch);
curl_close($ch);
return $response;
}