google app engine - Send JSon from Server to Client in GCM -


i using gcm (google cloud messaging).in want want send j son server side .on client side want receive simple message have done stucked how pass j son server side client side.

please me resolve this.

this server side code

 public class gcmbroadcast extends httpservlet {     private static final long serialversionuid = 1l;     private static final string sender_id = "";     private static final string android_device = "";     private list<string> androidtargets = new arraylist<string>();     public gcmbroadcast() {          super();         androidtargets.add(android_device);      }      protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {         string collapsekey = "";         string usermessage = "";          try {             usermessage = request.getparameter("message");             collapsekey = request.getparameter("collapsekey");         } catch (exception e) {             e.printstacktrace();             return;         }         sender sender = new sender(sender_id);         message message = new message.builder()           .collapsekey(collapsekey)         .adddata("message", usermessage)         .build();          try {              multicastresult result = sender.send(message, androidtargets, 1);             system.out.println("response: " + result.getresults().tostring());             if (result.getresults() != null) {                  int canonicalregid = result.getcanonicalids();                 if (canonicalregid != 0) {                     system.out.println("response " +canonicalregid );                 }             } else {                 int error = result.getfailure();                 system.out.println("broadcast failure: " + error);             }          } catch (exception e) {             e.printstacktrace();         }           request.setattribute("collapsekey", collapsekey);         request.setattribute("message", usermessage);          request.getrequestdispatcher("xx.jsp").forward(request, response);      }  } 

your payload (added message calls adddata) can name/value pairs. if want send json, can put json string in value of such name/value pair. you'll have parse json in client side.

for example :

.adddata("message","{\"some_json_key\":\"some_json_value\"}")


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -