0

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?

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
Somya
  • 13
  • 6

1 Answers1

0

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:

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

https://bitbucket.org/siani/connect2/src/b40d73d80ae308fb14e7fc28fd3331408591b1a4/connect2Server/DeviceServer/src/es/juandavidvega/connect2/server/device/sender/DataSenderService.java?at=master

  • I have edited my question with my error logs. Is there anything wrong with the way I have written my server? I try to run the server, but it crashes with the above errors. – Somya Aug 23 '15 at 12:04
  • I have googled it and I found this http://stackoverflow.com/questions/22629568/couldnt-load-memtrack-module-logcat-error apparently could be related with you emulator or device or something similar, follow the link instructions – Juan David Vega Rodriguez Aug 23 '15 at 12:25