【发布时间】:2014-09-18 08:52:29
【问题描述】:
因此,当有来自 Google Cloud Messaging (GCM) 的通知时,我正在尝试从我的服务器接收消息。我的代码如下:
public class GcmIntentService extends IntentService {
//This class is executed by the BradcastReceiver when the devices
//gets a message from GCM
public static final int NOTIFICATION_ID = 1;
private NotificationManager notificationManager;
NotificationCompat.Builder builder;
public GcmIntentService() {
super("GcmIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType();
if (!extras.isEmpty()) {
if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
Log.i("INTENTSERVICE", extras.toString());
sendNotification(extras);
}
Log.e("WAKELOCK","Wakelock will end");
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
}
private void sendNotification(final Bundle extras){
Socket socket = null;
FileOutputStream fileStream;
MessageSet ms = null;
try {
socket = new Socket(getString(R.string.server_address), 37133);
Tools.sendDataToServer(getApplicationContext(),
socket,
MessageTypes.PULLMESSAGE,
extras.getString("content")); //sending does perfectly work
//Receive contents of Message
DataInputStream dis =
new DataInputStream(socket.getInputStream()); // but now is doesn't
//want to do more and the exception is thrown
byte type = dis.readByte();
/* doing things */
} catch (IOException e) {
e.printStackTrace();
} finally {
//Tools.close(socket);
}
/* post notification */
}
}
我可以创建套接字并将数据发送到服务器,也收到了,但是当我想在电话上接收数据时,它会给出一个java.net.SocketException: Socket is closed 异常。但与此同时,服务器还在发送数据,手机上的socket也依然打开(according to debugger [PIC])。
但是为什么会出现这个错误呢?
Tools.sendDataToServer的请求码
public static void sendDataToServer(Context context, Socket socket, int messageType, String content){
try {
SharedPreferences prefs = context.getSharedPreferences(context.getString(R.string.SharedPrefs),0);
OutputStream out = socket.getOutputStream();
out.write(messageType);
byte[] msgBytes = (prefs.getString("userID","") + "|;" + prefs.getString("userSession","") + "|;" + content).getBytes("UTF-8");
Tools.writeDataLengthToStream(out, msgBytes.length);
out.write(msgBytes,0,msgBytes.length);
out.flush();
//out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
当out.close() 被注释掉时,它正在工作。这是我已经遇到的问题,但我仍然不知道为什么会这样。
【问题讨论】:
-
Tools.sendDataToServer 是做什么的?你能发布那个代码吗?
-
@eldjon :这就是代码,它可以在没有 out.close() 行的情况下工作。
标签: java android sockets exception