[英]Android: GCM Java server keeps shutting down
Can somebody please tell me why this Java server that I'm trying to build which connects to the GCm server to send messages to the reg_id, keeps crashing? 有人可以告诉我为什么我要构建的该Java服务器连接到GCm服务器以将消息发送到reg_id时,该服务器始终崩溃吗?
I am very new to building my own client-server classes. 我对建立自己的客户端-服务器类非常陌生。 And I could easily be missing out something very silly/crucial.
而且我很容易错过一些非常愚蠢/至关重要的事情。
Let me know if more details are needed. 让我知道是否需要更多详细信息。 Thanks!
谢谢!
package com.example.smittal.gcmserver;
import android.util.Log;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class Server {
public static final String API_KEY = "***";
private static final String TAG = "Server";
private static final String regId = "***";
private static final String msg = "Yeh message hai.";
private static final String title = "Yeh Title hai.";
public static void main(String[] args) {
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
try {
JSONObject obj = new JSONObject("{\"data\":{\"title\":" + title + ",\"message\":" + msg + "},\"registration_ids\":[" + regId + "]}");
/* JSONObject jGCMData = new JSONObject();
JSONObject jData = new JSONObject();
JSONObject jTitle = new JSONObject();
jTitle.put("title", msg);
jData.put("data", jTitle);
jGCMData.put("registration_ids", regId);
Log.i(TAG, jGCMData.toString());
*/
Log.i(TAG, obj.toString());
URL url = new URL("https://android.googleapis.com/gcm/send");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Authorization", "key=" + API_KEY);
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setDoOutput(true);
OutputStream outputStream = urlConnection.getOutputStream();
//outputStream.write(jGCMData.toString().getBytes());
outputStream.write(obj.toString().getBytes());
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
// Since it's JSON, adding a newline isn't necessary (it won't affect parsing)
// But it does make debugging a *lot* easier if you print out the completed
// buffer for debugging.
buffer.append(line + "\n");
}
Log.i(TAG, buffer.toString());
} catch (java.io.IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
} }
EDIT: This is the error I'm getting. 编辑:这是我得到的错误。
08-23 17:29:22.697 5255-5255/? D/AndroidRuntime﹕ >>>>>> START com.android.internal.os.RuntimeInit uid 0 <<<<<<
08-23 17:29:22.698 5255-5255/? D/AndroidRuntime﹕ CheckJNI is ON
08-23 17:29:22.723 5255-5255/? E/memtrack﹕ Couldn't load memtrack module (No such file or directory)
08-23 17:29:22.723 5255-5255/? E/android.os.Debug﹕ failed to load memtrack module: -2
08-23 17:29:22.742 5255-5255/? D/AndroidRuntime﹕ Calling main entry com.android.commands.am.Am
08-23 17:29:22.743 1219-1369/system_process I/ActivityManager﹕ Force stopping com.example.smittal.gcmserver appid=10066 user=0: from pid 5255
08-23 17:29:22.749 5255-5255/? D/AndroidRuntime﹕ Shutting down VM
08-23 17:29:22.749 5255-5258/? I/art﹕ Debugger is no longer active
08-23 17:29:22.750 5255-5264/? E/art﹕ Thread attaching while runtime is shutting down: Binder_1
08-23 17:29:22.750 5255-5264/? I/AndroidRuntime﹕ NOTE: attach of thread 'Binder_1' failed
I am not sure what is your error but if you want to check how I used to register device on the GCM Server you can check this code: 我不确定您的错误是什么,但是如果您想查看我以前在GCM服务器上注册设备的方式,可以检查以下代码:
https://bitbucket.org/siani/connect2/src/b40d73d80ae308fb14e7fc28fd3331408591b1a4/SimpleLauncher/app/src/main/java/es/juandavidvega/conect2app/launcher/LoginActivity.java?at=master https://bitbucket.org/siani/connect2/src/b40d73d80ae308fb14e7fc28fd3331408591b1a4/SimpleLauncher/app/src/main/java/es/juandavidvega/conect2app/launcher/LoginActivity.java?at=master
And you can also check how I use GCM library to send the messages 您还可以检查我如何使用GCM库发送消息
https://bitbucket.org/siani/connect2/src/b40d73d80ae308fb14e7fc28fd3331408591b1a4/connect2Server/DeviceServer/src/es/juandavidvega/connect2/server/device/sender/DataSenderService.java?at=master https://bitbucket.org/siani/connect2/src/b40d73d80ae308fb14e7fc28fd3331408591b1a4/connect2Server/DeviceServer/src/es/juandavidvega/connect2/server/device/sender/DataSenderService.java?at=master
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.