Android:GCM Java服务器不断关闭

时间:2015-08-23 08:03:21

标签: java server google-cloud-messaging

有人可以告诉我为什么我正在尝试构建连接到GCm服务器的Java服务器向reg_id发送消息,不断崩溃?

我非常擅长构建自己的客户端 - 服务器类。我很容易错过一些非常愚蠢/至关重要的东西。

如果需要更多详细信息,请与我们联系。谢谢!

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();
    }


}

}

编辑:这是我得到的错误。

   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