Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementation of GCM in Android

I am implementing GCM in android..I followed the steps given in "http://hmkcode.com/android-google-cloud-messaging-tutorial/" . I got the regId from the android side.I entered that in the Java Project(eclipse). In Java project i imported the jackson-all-1.9.0.jar. I have 3 java files in total in this JavaProject

1) Content.java

import java.io.Serializable;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class Content implements Serializable{
    private List<String> registration_ids;
    private Map<String,String> data;

    public void addRegId(String regId){
        if(registration_ids == null)
            registration_ids = new LinkedList<String>();
        registration_ids.add(regId);
    }

    public void createData(String title, String message){
        if(data == null)
            data = new HashMap<String,String>();

        data.put("title", title);
        data.put("message", message);
}
}

2) POST2GCM.java

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;


import org.codehaus.jackson.map.ObjectMapper;


public class POST2GCM {

    public static void post(String apiKey, Content content){

        try{

        // 1. URL
        URL url = new URL("https://android.googleapis.com/gcm/send");

        // 2. Open connection
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        // 3. Specify POST method
        conn.setRequestMethod("POST");

        // 4. Set the headers
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setRequestProperty("Authorization", "key="+apiKey);

        conn.setDoOutput(true);

            // 5. Add JSON data into POST request body

            //`5.1 Use Jackson object mapper to convert Contnet object into JSON
            ObjectMapper mapper = new ObjectMapper();

            // 5.2 Get connection output stream
            DataOutputStream wr = new DataOutputStream(conn.getOutputStream());

            // 5.3 Copy Content "JSON" into

            mapper.writeValue(wr, content);

            // 5.4 Send the request
            wr.flush();

            // 5.5 close
            wr.close();

            // 6. Get the response
            int responseCode = conn.getResponseCode();
            System.out.println("\nSending 'POST' request to URL : " + url);
            System.out.println("Response Code : " + responseCode);

            BufferedReader in = new BufferedReader(
                    new InputStreamReader(conn.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            // 7. Print result
            System.out.println(response.toString());

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
}

3)App.java

public class App {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println( "Sending POST to GCM" );

        String apiKey = "my api key which i got from //console.developers.google.com";
        Content content = createContent();

        POST2GCM.post(apiKey, content);
    }
     public static Content createContent(){

            Content c = new Content();

            c.addRegId("MyRegKey which i got from android side");
            c.createData("Test Title", "Test Message");

            return c;
        }
}

The error i am getting is :

org.codehaus.jackson.map.JsonMappingException: No serializer found for class Content and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS) )
    at org.codehaus.jackson.map.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:52)
    at org.codehaus.jackson.map.ser.impl.UnknownSerializer.serialize(UnknownSerializer.java:25)
    at org.codehaus.jackson.map.ser.StdSerializerProvider._serializeValue(StdSerializerProvider.java:610)
    at org.codehaus.jackson.map.ser.StdSerializerProvider.serializeValue(StdSerializerProvider.java:256)
    at org.codehaus.jackson.map.ObjectMapper._configAndWriteValue(ObjectMapper.java:2575)
    at org.codehaus.jackson.map.ObjectMapper.writeValue(ObjectMapper.java:2065)
    at POST2GCM.post(POST2GCM.java:45)
    at App.main(App.java:14)

Can any one please help me on this? i did not find any specific answer for this..Tried the once posted but those did not solve the issue, this error was constant. Thank you in advance.

like image 582
Tanmay Pandharipande Avatar asked Jan 17 '26 22:01

Tanmay Pandharipande


1 Answers

Make variables registration_ids and data to public in Content class.

public class Content implements Serializable {
    public List<String> registration_ids;
    public Map<String,String> data;

as jackson api reads the public variables from class or public getter/setters and in your case the variables are not public and also you didn't provide getter/setters, so the exception is.

Hope it'll work.

like image 192
ELITE Avatar answered Jan 19 '26 12:01

ELITE



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!