2016-03-22 62 views
2

我在android..I中執行GCM,遵循「http://hmkcode.com/android-google-cloud-messaging-tutorial/」中給出的步驟。 我從android側得到了regId。我在Java Project(eclipse)中輸入了它。在Java項目中,我導入了jackson-all-1.9.0.jar。 我在這個JavaProject在Android中執行GCM

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; 
     } 
} 
總共3個java文件

我得到的錯誤是:

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) 

任何人都可以幫助我嗎? 我沒有找到任何具體的答案這個..嘗試一次發佈,但沒有解決問題,這個錯誤是不變的。 預先感謝您。

回答

1

使變量registration_idsdataContent類中公開。

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

jackson API讀取類或公有的getter/setter方法和你的情況公共變量變量是不公開的,你也沒提供getter/setter方法,所以異常是。

希望它能工作。

+0

@NiRRaNjAN RauT – ELITE