Kurono_Kei
Kurono_Kei

Reputation: 23

Jackson Unhandled Exception?

I am new to android programming, and I was following this tutorial to create a GCM server program. However, I came across a frustrating bug and would greatly appreciate any help.

This is my POST2GCM class:

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 com.fasterxml.jackson.databind.ObjectMapper;


public class POST2GCM extends Content {

private static final long serialVersionUID = 1L;

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

I have included the "jackson-databind-2.5.1.jar" file but I get the error:

Unhandled Exception: com.fasterxml.jackson.databind.JsonMappingException

on the line mapper.writeValue(wr, content);

What is causing this exception, and how can I fix it?

Upvotes: 2

Views: 16364

Answers (1)

Arkar Aung
Arkar Aung

Reputation: 3584

jackson-databind is a general data-binding package which works on streaming API (jackson-core) implementations. That's why you need to add jackson-core and catch 3 exceptions. writeValue method throws IOException, JsonGenerationException and JsonMappingException.

try {
    mapper.writeValue(wr, content);
} catch (JsonMappingException e) {
    e.printStackTrace();
} catch (JsonGenerationException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

Hope it will be useful for you.

Upvotes: 5

Related Questions